1

I am using this meta query inside a category:

$args   =  array(
    "posts_per_page"    => 12,
    "post_type"         => array( "event", "post" ),
    "post_status"       => "publish",
    "category"          => $tid,
    'meta_query'        => array(
                                'relation' => 'OR',
                                array(
                                    'key'       => 'event_start',
                                    'value'     => date("Y-m-d H:i:s"),
                                    'compare'   => '>=',
                                    'type'      => 'DATE'
                                ),
                                array(
                                    'key'       => 'event_start',
                                    'compare'   => 'NOT EXISTS'
                                )
                            ),
                            array(
                                'key'       => '_thumbnail_id',
                                'compare'   => 'EXISTS'
                            ),
                            array(
                                'key'       => 'foreign_language',
                                'value'     => 0
                            )
);

$posts          = get_posts( $args );

It should return 12 posts, but return only 11. If I set posts_per_page to 13, it returns 12.

What could be the issue?

  • Change $posts = get_posts( $args ); to something like $posts_array = get_posts( $args ); ;-) – Pieter Goosen Jan 31 '16 at 15:11
0

$posts is a reserved global variables which stores the value from the $posts property from the main query. By using $posts as a local variable, you are breaking the main query and the $posts global variable. This leads to unexpected output.

You should use unique custom variables as local variables. You can solve your issue by changing

$posts = get_posts( $args );

to something like

$posts_array = get_posts( $args );

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.