0

I am trying to show the featured posts in my theme, with a custom field mytheme_featured_post which is 1 on the featured posts.

However it does not seem to filter the posts down to only the posts in the meta query.

// WP_Query arguments.
$featured = array(
'posts_per_page' => '5',
'meta_query' => array(
    array(
        'key'     => 'mytheme_featured_post',
        'value'   => '1',
    ),
),
);

// The Query.
$featured_query = new WP_Query( $featured );

if ( $featured_query -> have_posts() ) {
    while ( $featured_query -> have_posts() ) : $featured_query -> the_post();
        the_title();
    endwhile;
}

Update: This args are working as intended:

// WP_Query arguments.
$featured = array(
    'posts_per_page' => '5',
    'cat'            => '1',
    'meta_key'       => 'mytheme_featured_post',
    'meta_value'     => '1',
);
3
  • What does it show? Does it show the data from previous query?
    – Johansson
    Commented Jul 23, 2017 at 21:28
  • Seems to be the main query which displays, @JackJohansson. Commented Jul 23, 2017 at 21:34
  • Check your theme's functions.php file for any pre_get_posts filter. It might be messing with your queries.
    – Johansson
    Commented Jul 24, 2017 at 9:50

2 Answers 2

0

First make sure your query current or not.

Ref - https://codex.wordpress.org/Class_Reference/WP_Query

After or before every custom query, write query reset function.

wp_reset_query()

Try it.

3
  • 1
    You don't need that before and after every custom query, you only need it after overwriting the global $wp_query variable. Look at the source code and see what it actually does.
    – Milo
    Commented Jul 24, 2017 at 14:53
  • Does get_posts() alter the main query data? I had a custom get_posts() in my template, and after pulling my hairs out for a week I noticed that because I'm not using wp_reset_postdata() it's messing with my query. @Milo
    – Johansson
    Commented Jul 24, 2017 at 21:10
  • 1
    @JackJohansson If you use setup_postdata() or $query->the_post() then you need wp_reset_postdata() after to restore the global $post. wp_reset_query() also works in this case because it calls wp_reset_postdata(), but it also restores $wp_query, which may have unwanted side-effects if that's not what you're expecting.
    – Milo
    Commented Jul 24, 2017 at 21:21
0

I was tricked by sticky posts. Here is the working code if anyone coming by for the same problem! Check if it works after excluding the stickies.

/**
 * WP_Query arguments.
 *
 * @link https://codex.wordpress.org/Class_Reference/WP_Query
 */
$args = array(
    'posts_per_page' => '4',
    'orderby' => 'modified',
    'order' => 'desc',
    'post__not_in'   => get_option( 'sticky_posts' ),
    'meta_query' => array(
        array(
            'key'     => 'mytheme_featured_post',
            'value'   => '1',
            ),
            ),
    );

// The Query.
$query = new WP_Query( $args );

// The Loop.
if ( $query->have_posts() ) {

    while ( $query->have_posts() ) :
        $query->the_post();
        the_title();
    endwhile;
}
wp_reset_postdata();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.