• Hi everyone. I am designing a theme for WordPress and I need to create custom $wp_query calls in order to load in different post formats in different parts of the website. I write the following code:

    <?php
    $args = array(
       'post_type'=> 'post',
       'tax_query' => array(
       array(
          'taxonomy' => 'post_format',
          'field' => 'slug',
          'terms' => array( 'post-format-aside' )
       )
       )
    );
    $the_query = new WP_Query($args);
    ?>
    
    <?php if ( $the_query->have_posts() ) : ?>
    
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <?php
       get_template_part( 'content-custom-posts', get_post_format() );
    ?>
    <?php endwhile; ?>

    My problem is that in the content-custom-posts.php I do a check

    if($the_query->current_post == 0){...}
    else {...}

    However, for some reason even though the while loop loops through all my posts, the expression above ALWAYS evaluates to true, as if the current_post is not incrementing. I echoed $the_query->current_post and it gives back 0 1 2 3 4 … if I put it right before the get_template_part in the first code above, but DOES NOT RETURN ANYTHING if I echo it from within content-custom-posts.php

    I searched all over the web and I cannot find a solution to my problem. Is my wp_query wrong for some reason? Because it works with $wp_query (default) but I need to do custom queries…

    Thank you!

Viewing 1 replies (of 1 total)
  • Thread Starter malyutad

    (@malyutad)

    UPDATE : I just checked and in fact if I change the name $the_query to $wp_query, keeping all the arguments the same, it works. What is going on!? Why is the query variable name ($the_query) stopping my code from working?

Viewing 1 replies (of 1 total)
  • The topic ‘$custom_query->current_post not working!?!?’ is closed to new replies.