admin管理员组文章数量:1130349
In the context of a Wordpress widget development, I use WP_Query this way:
$args = array(
'post_type' => 'myType',
'post_per_page' => $number,
'showposts' => $number
);
$latest_myTypes = new WP_Query($args);
if ($latest_myTypes->have_posts()):
global $post; //etc.
When a myType post is saved, it stores some values in the table wp_postmeta, in particular two keys: day (an integer) and month (a string).
As you can see, in the preceding code I get all the posts of type myType.
I need a different thing: since any single post contains upcoming events, I want to get all the posts where the date is equal or after the current date (so, day must be at least today AND the month should be at least the current month).
Is it possible to modify my WP_Query to filter the posts this way?
I've red about meta_query but I've not understood how to integrate a meta query to my current query, and even how to create the query itself.
In pseudocode, I should get all the posts WHERE:
day >= date("j")
AND
date("n", strtotime(month)) >= date("n")
Can you help me?
Update: I know it would be much easier if I could store the Unix timestamp of an upcoming event date: problem is I inherited code from another developer and found that he used metaboxes to automatically save data. I couldn't find a way to alter his code to save day, month and year submitted by a user as a combined and unique key value, since he uses three different fields (it would be interesting if I could create a sort of computed field)
Update: I suppose I solved this way:
$args = array(
'post_type' => 'myType',
'post_per_page' => $number,
'showposts' => $number,
'meta_query' => array(
array(
'key' => 'month',
'value' => array(date("F"),date("F", strtotime('+1 month'))),
'compare' => 'IN'
),
array(
'key' => 'day',
'value' => date("j"),
'compare' => '>='
),
array(
'key' => 'year',
'value' => date("Y"),
'compare' => '='
),
),
);
It is working fine. Any other comment, solution, suggestion, etc. is eventually appreciated.
In the context of a Wordpress widget development, I use WP_Query this way:
$args = array(
'post_type' => 'myType',
'post_per_page' => $number,
'showposts' => $number
);
$latest_myTypes = new WP_Query($args);
if ($latest_myTypes->have_posts()):
global $post; //etc.
When a myType post is saved, it stores some values in the table wp_postmeta, in particular two keys: day (an integer) and month (a string).
As you can see, in the preceding code I get all the posts of type myType.
I need a different thing: since any single post contains upcoming events, I want to get all the posts where the date is equal or after the current date (so, day must be at least today AND the month should be at least the current month).
Is it possible to modify my WP_Query to filter the posts this way?
I've red about meta_query but I've not understood how to integrate a meta query to my current query, and even how to create the query itself.
In pseudocode, I should get all the posts WHERE:
day >= date("j")
AND
date("n", strtotime(month)) >= date("n")
Can you help me?
Update: I know it would be much easier if I could store the Unix timestamp of an upcoming event date: problem is I inherited code from another developer and found that he used metaboxes to automatically save data. I couldn't find a way to alter his code to save day, month and year submitted by a user as a combined and unique key value, since he uses three different fields (it would be interesting if I could create a sort of computed field)
Update: I suppose I solved this way:
$args = array(
'post_type' => 'myType',
'post_per_page' => $number,
'showposts' => $number,
'meta_query' => array(
array(
'key' => 'month',
'value' => array(date("F"),date("F", strtotime('+1 month'))),
'compare' => 'IN'
),
array(
'key' => 'day',
'value' => date("j"),
'compare' => '>='
),
array(
'key' => 'year',
'value' => date("Y"),
'compare' => '='
),
),
);
It is working fine. Any other comment, solution, suggestion, etc. is eventually appreciated.
Share Improve this question edited Nov 11, 2018 at 12:55 asked Nov 10, 2018 at 16:21 user140189user1401891 Answer
Reset to default 0This is the answer to my question:
$args = array(
'post_type' => 'myType',
'post_per_page' => $number,
'showposts' => $number,
'meta_query' => array(
array(
'key' => 'month',
'value' => array(date("F"),date("F", strtotime('+1 month'))),
'compare' => 'IN'
),
array(
'key' => 'day',
'value' => date("j"),
'compare' => '>='
),
array(
'key' => 'year',
'value' => date("Y"),
'compare' => '='
),
),
);
In the context of a Wordpress widget development, I use WP_Query this way:
$args = array(
'post_type' => 'myType',
'post_per_page' => $number,
'showposts' => $number
);
$latest_myTypes = new WP_Query($args);
if ($latest_myTypes->have_posts()):
global $post; //etc.
When a myType post is saved, it stores some values in the table wp_postmeta, in particular two keys: day (an integer) and month (a string).
As you can see, in the preceding code I get all the posts of type myType.
I need a different thing: since any single post contains upcoming events, I want to get all the posts where the date is equal or after the current date (so, day must be at least today AND the month should be at least the current month).
Is it possible to modify my WP_Query to filter the posts this way?
I've red about meta_query but I've not understood how to integrate a meta query to my current query, and even how to create the query itself.
In pseudocode, I should get all the posts WHERE:
day >= date("j")
AND
date("n", strtotime(month)) >= date("n")
Can you help me?
Update: I know it would be much easier if I could store the Unix timestamp of an upcoming event date: problem is I inherited code from another developer and found that he used metaboxes to automatically save data. I couldn't find a way to alter his code to save day, month and year submitted by a user as a combined and unique key value, since he uses three different fields (it would be interesting if I could create a sort of computed field)
Update: I suppose I solved this way:
$args = array(
'post_type' => 'myType',
'post_per_page' => $number,
'showposts' => $number,
'meta_query' => array(
array(
'key' => 'month',
'value' => array(date("F"),date("F", strtotime('+1 month'))),
'compare' => 'IN'
),
array(
'key' => 'day',
'value' => date("j"),
'compare' => '>='
),
array(
'key' => 'year',
'value' => date("Y"),
'compare' => '='
),
),
);
It is working fine. Any other comment, solution, suggestion, etc. is eventually appreciated.
In the context of a Wordpress widget development, I use WP_Query this way:
$args = array(
'post_type' => 'myType',
'post_per_page' => $number,
'showposts' => $number
);
$latest_myTypes = new WP_Query($args);
if ($latest_myTypes->have_posts()):
global $post; //etc.
When a myType post is saved, it stores some values in the table wp_postmeta, in particular two keys: day (an integer) and month (a string).
As you can see, in the preceding code I get all the posts of type myType.
I need a different thing: since any single post contains upcoming events, I want to get all the posts where the date is equal or after the current date (so, day must be at least today AND the month should be at least the current month).
Is it possible to modify my WP_Query to filter the posts this way?
I've red about meta_query but I've not understood how to integrate a meta query to my current query, and even how to create the query itself.
In pseudocode, I should get all the posts WHERE:
day >= date("j")
AND
date("n", strtotime(month)) >= date("n")
Can you help me?
Update: I know it would be much easier if I could store the Unix timestamp of an upcoming event date: problem is I inherited code from another developer and found that he used metaboxes to automatically save data. I couldn't find a way to alter his code to save day, month and year submitted by a user as a combined and unique key value, since he uses three different fields (it would be interesting if I could create a sort of computed field)
Update: I suppose I solved this way:
$args = array(
'post_type' => 'myType',
'post_per_page' => $number,
'showposts' => $number,
'meta_query' => array(
array(
'key' => 'month',
'value' => array(date("F"),date("F", strtotime('+1 month'))),
'compare' => 'IN'
),
array(
'key' => 'day',
'value' => date("j"),
'compare' => '>='
),
array(
'key' => 'year',
'value' => date("Y"),
'compare' => '='
),
),
);
It is working fine. Any other comment, solution, suggestion, etc. is eventually appreciated.
Share Improve this question edited Nov 11, 2018 at 12:55 asked Nov 10, 2018 at 16:21 user140189user1401891 Answer
Reset to default 0This is the answer to my question:
$args = array(
'post_type' => 'myType',
'post_per_page' => $number,
'showposts' => $number,
'meta_query' => array(
array(
'key' => 'month',
'value' => array(date("F"),date("F", strtotime('+1 month'))),
'compare' => 'IN'
),
array(
'key' => 'day',
'value' => date("j"),
'compare' => '>='
),
array(
'key' => 'year',
'value' => date("Y"),
'compare' => '='
),
),
);
本文标签: wp queryFiltering WPQuery based on wppostmeta keys values
版权声明:本文标题:wp query - Filtering WP_Query based on wp_postmeta keys values 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749183051a2328931.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论