• Resolved gicoz

    (@gicoz)


    Hi
    I have this function with WP query and some args, it give the correct number of items found ( 1), but the loop gives me 4 items ( all the stickys post)

    $args = array(
    ‘post_type’ => ‘post’,
    ‘meta_query’ => array(
    ‘relation’ => ‘AND’,
    array(
    ‘key’ => ‘rubriques’,
    ‘value’ => ‘178’,
    ‘compare’ => ‘LIKE’,
    ),
    
    ),
    ‘post__in’ =>get_option( ‘sticky_posts’ )
    );
    
    $caroussel_query = new WP_Query( $args );
    
    echo ‘<br>’ . $caroussel_query->found_posts . ‘<br>’;
    
    if ( $caroussel_query->have_posts() ) {
    
    while ( $caroussel_query->have_posts() ) {
    
    $caroussel_query->the_post();
    
    echo the_ID() . ‘<br>’;
    }
    }

    query->found_posts give 1. it is correct

    but the loop shows 4 ids.
    Please note that the “rubriques” field is an ACF serialized array

    Thanks for your help
    Gilles

    Gilles

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Yup, WP by default always adds sticky posts to the beginning of all query post lists, matching or not. To prevent this, add 'ignore_sticky_posts' => true, to your $args array.

    Incidentally, the code you posted is unusable for testing because the quotes got converted to ‘curly’ type along the line somewhere. They must be 'straight' in valid code. There was no need for testing here, but any future posts you might make could require testing. In the future, please ensure the pasted code does not get corrupted and always demarcate code by using backticks, or use the code button. It helps us help you 🙂

    Thread Starter gicoz

    (@gicoz)

    Hi bcwork and thanks for your help, this thing almost drove me crazy. Sorry about the , i tried to make easely understandable…
    Again,thanks a lot.
    Gilles

    Moderator bcworkz

    (@bcworkz)

    You’re welcome. No worries about the quote thing, it’s just FYI. We can’t expect you to do anything differently if you are unaware there was a problem 🙂

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘WP_Query strange results’ is closed to new replies.