I am trying to grab the 5 most recent posts and have it:
- exclude posts in an array that is already defined ($exclude)
- ignore sticky posts
- ignore any posts that have a custom field checked off as a featured post (i.e. with the custom field "my_featured_post" set to 1)
Can someone please build this query for me. If it works the money is yours. :)
Gabriel Merovingi answers:
$my_query_arg = array(
'posts_per_page' => 5, // number of posts to return
'post__not_in' => array(1,2,3,4), // array of IDs to exclude example ids 1,2,3 and 4
'ignore_sticky_posts' => 1, // ignore sticky posts
'meta_query' => array(
array(
'key' => 'my_featured_post', // meta query where my_featured_post is not 1
'value' => '1',
'compare' => 'NOT LIKE'
)
)
);
// Run Query
$my_query = new WP_Query( $my_query_arg );
if ( $my_query->have_posts() ):
while( $my_query->have_posts() ) : $my_query->the_post();
// Your code here
endwhile;
endif;
// Always reset once done
wp_reset_postdata();
For more information visit the WP Codex for [[LINK href="http://codex.wordpress.org/Class_Reference/WP_Query"]]WP_Query[[/LINK]]
mackrider comments:
see my reply below to Martin Pham
Gabriel Merovingi comments:
So the exclude bit works? Just not picking up on the meta query?
Martin Pham answers:
try this
$sticky = get_option( 'sticky_posts' );
$args = array(
'posts_per_page' => 10,
'ignore_sticky_posts ' => 1,
'post__not_in' => $sticky,
'meta_key' => 'my_featured_post',
'meta_value' => '1',
'meta_compare' => '!='
);
$query = new WP_Query($args);
Martin Pham comments:
Full code here :)
$exclude = array();
$sticky = get_option( 'sticky_posts' );
$ignore_post = wp_parse_args($exclude,$sticky);
$args = array(
'posts_per_page' => 10,
'ignore_sticky_posts ' => 1,
'post__not_in' => $ignore_post,
'meta_key' => 'my_featured_post',
'meta_value' => '1',
'meta_compare' => '!='
);
$query = new WP_Query($args);
Martin Pham comments:
More information:
Custom field query: [[LINK href="http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters"]]http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters[[/LINK]]
Sticky Post Parameters: [[LINK href="http://codex.wordpress.org/Class_Reference/WP_Query#Sticky_Post_Parameters"]]http://codex.wordpress.org/Class_Reference/WP_Query#Sticky_Post_Parameters[[/LINK]]
Post Parameters: [[LINK href="http://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters"]]http://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters[[/LINK]]
mackrider comments:
Unfortunately the solutions are not seeming to work.
I tried those - and I tried
$args = array('post__not_in' => $exclude,
'ignore_sticky_posts' => 1,
'posts_per_page' => 5,
'meta_query'=> array(
array(
'key'=>'my_featured_post',
'value'=> 1,
'type' => 'numeric', // assuming your custom_value is an int, not a string.
'compare'=>'!='
)
)
);
$query = new WP_Query($args);
to no avail.... I was thinking maybe i have the wrong key - but when I set compare to just "=" I get the same exact results instead of a subset of just featured posts, see screenshot from a featured post's custom fields