4

I cannot figure out why my WP_Query always displays all my published posts regardless of what I put in the arguments.

<?php 

    $args = array('numberposts' => 1,
                    'meta_key' => 'display',
                    'meta_value' => 'about'
    );

    $about_preview_query = new WP_Query($args);

    if ($about_preview_query->have_posts()) {
        print "<h1>FOUND POSTS</h1>";
    }

    while ($about_preview_query->have_posts()) {
        $about_preview_query->the_post();
        print "<h1>";
        the_title();

        print "</h1>";

    }
?>

Am I doing something wrong here? After readings the docs on WP_Query() I cannot figure out where I'm going wrong, appreciate any help I can get.

Update

I've tried this code and I am still getting the same response. All the posts are being returned in the loop.

    $args = array(
        'posts_per_page' => 1,
        'meta_query' => array(
            array(
                'key' => 'display',
                'value' => 'about',
                'compare' => '=',
             )
        ),
    );

Update 2

It seems if I print $about_preview_query->found_posts the output is 1. So I suspect there is something wrong with how I am looping the posts:

<?php if ($about_preview_query->have_posts()): ?>

    <h1>Has <?php print $about_preview_query->found_posts ?> Posts</h1>

    <?php while ($about_preview_query->have_posts()): $about_preview_query->the_post(); ?>
        <h2><?php the_title(); ?></h2>
    <?php endwhile; ?>

<?php endif; ?>
6
  • Have you tried resetting your postdata? Oct 3 '14 at 7:43
  • Hey, thanks for your reply. I literally just tried putting wp_reset_postdata(); above my query a second before your comment. Still I get the same issue where it's outputting all the posts. Oct 3 '14 at 7:46
  • Below your query, do a var_dump inside a pre tag and see what is returned by your query. Example ?><pre><?php var_dump($about_preview_query); ?></pre><?php Oct 3 '14 at 7:51
  • Strange, even though the found_posts count is 1, the var dump suggests the query is returning all posts :S. Oct 3 '14 at 7:57
  • Then there are posts injected from somewhere else. Somewhere you are running custom queries that has not being resetted. It might even be from an instance of pre_get_posts Oct 3 '14 at 8:06
3

It is because you are giving wrong arguments. Provide right arguments to WP_Query().

Also have a look at the custom field parameters

One more thing, you will have to use code like

$args = array(
    'posts_per_page' => 5,
    'meta_query' => array(
        array(
            'key'     => 'display',
            'value'   => 'about',
            'compare' => '=', // (Read all comparison operators at provided link)
        ),
    ),
);
0
2

There is no parameter for numberposts. Use posts_per_page instead. So your code will become...

<?php 

    $args = array(
        'posts_per_page' => 1,
        'meta_key' => 'display',
        'meta_value' => 'about'
    );

    $about_preview_query = new WP_Query($args);

    if ( $about_preview_query->have_posts() ) {
        print "<h1>FOUND POSTS</h1>";
    }

    while ( $about_preview_query->have_posts() ) {
        $about_preview_query->the_post();
        print "<h1>";
        the_title();
        print "</h1>";
    }
?>
2

All you need to do is change

'posts_per_page' => 1,

to

'posts_per_page' => -1,
1
  • 2
    -1 would return all posts.
    – Picard
    Jun 26 '17 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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