• Hi there,

    I am trying to display the latest post from a specific category as a featured post.

    I have this code:

    	<?php $category_id = get_cat_ID( 'Competitions' );
              $args = array(
                  'cat'       => $category_id,
                  'post__in'  => get_option('sticky_posts'),
              );
        ?>
    	<?php $the_query = new WP_Query($args);
              $the_query = new WP_Query( 'posts_per_page=1' );?>
    
    	<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>

    which is supposed to get the post from the post category “Competitions”

    This works fine, except when I add the same code and change the category to “Interviews”, both blocks then display the same post from “Interviews”

    	<?php $category_id = get_cat_ID( 'Interviews' );
              $args = array(
                  'cat'       => $category_id,
                  'post__in'  => get_option('sticky_posts'),
              );
        ?>
    	<?php $the_query = new WP_Query($args);
              $the_query = new WP_Query( 'posts_per_page=1' );?>
    
    	<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>

    Can anyone see what I am doing wrong with my code?

    Thanks!

Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter thetoolman123

    (@thetoolman123)

    Hi,

    No they are assigned to individual categories.

    1 is in Competitions and 1 is in Interviews

    Moderator bcworkz

    (@bcworkz)

    The new WP_Query( 'posts_per_page=1' ); line is overriding the previous query using $args. Thus the same results each time as it’s the same query in effect each time. Combine all the args into one array.

    The ‘post__in’ arg could prevent the desired posts from being found, unless the sticky posts are within the desired category. And if they are, you will only get the first sticky post every time, not the latest post. If you want to do a latest post query but want to also include sticky posts, do two separate queries. One to get the stickies, then another to get the latest.

    Perhaps you’ve done so but didn’t include it here, you should call wp_reset_postdata(); when you’re done with each query.

    Thread Starter thetoolman123

    (@thetoolman123)

    I see, so if I were to use $args and $argsTwo for example, it should work?

    And the wp_reset_postdata(); would go at the end of each query?

    Moderator bcworkz

    (@bcworkz)

    Not if you still have $the_query = new WP_Query( 'posts_per_page=1' ); in place. Add 'posts_per_page' => 1, to each $args array and remove the above line in each section. It doesn’t matter if you reused $args or $the_query for multiple queries as long as they are assigned the correct values for each use.

    Reset post data before the next query and after any code that relies upon the current query object. Usually this is after each while (){...} loop completes. Like this:

    endwhile;
    wp_reset_postdata();
    
    $category_id = get_cat_ID( 'Interviews' );
              $args = array(
                  'cat'       => $category_id,
                  // etc....
    Thread Starter thetoolman123

    (@thetoolman123)

    Thanks for the reply.

    I am not sure if I have this correct as it didn’t seem to work.

    This is now what I have:

    <?php $category_id = get_cat_ID( 'competitions' );
              $args = array(
                  'posts_per_page' => 1,
                  'cat'       => $category_id,
                  'post__in'  => get_option('sticky_posts'),
              );
        ?>
    	<?php $the_query = new WP_Query($args);
              $the_query = new WP_Query( 'posts_per_page=1' );?>
    
    	<?php while ($the_query -> have_posts()) : $the_query -> the_post(); wp_reset_postdata();?>
    
    
    
    
    
    <?php $category_id = get_cat_ID( 'interviews' );
              $argstwo = array(
                  'posts_per_page' => 1,
                  'cat'       => $category_id,
                  'post__in'  => get_option('sticky_posts'),
              );
        ?>
    	<?php $the_query = new WP_Query($argstwo);
              $the_query = new WP_Query( 'posts_per_page=1' );?>
    
    	<?php while ($the_query -> have_posts()) : $the_query -> the_post(); wp_reset_postdata();?>
    Thread Starter thetoolman123

    (@thetoolman123)

    I think I have worked it out. This is what I now have:

    <?php // Get the category ID
    $category_id = get_cat_ID( 'interviews' );
    
    // Get the arguments for the WP_Query
    $args = [
      'cat' => $category_id,
      'posts_per_page' => 1,
    ];
    
    // Create the WP_Query object
    $the_query = new WP_Query( $args );
    
    // Loop through the posts
    while ( $the_query->have_posts() ) {
      $the_query->the_post();
    }
    ?>
    HTML STUFF HERE
    <?php 
    // Reset the postdata
    wp_reset_postdata(); ?>
    Moderator bcworkz

    (@bcworkz)

    Looks good! It does what you want, correct? Then you needn’t do a thing more. But you could streamline it a bit if you were so inclined. There’s no need to if it works, but striving for clear, concise code is a good thing.

    WP_Query will directly accept a term slug arg without you needing to use get_cat_ID(). I assume the “interviews” category name is also its slug value. You could do this:

    <?php // Get the arguments for the WP_Query
    $args = [
      'category_name' => 'interviews',
      'posts_per_page' => 1,
    ];

    Despite the arg ‘category_name’, you should assign the slug value, not the term’s display name.

    This change will streamline your code a bit. OTOH, further changes to your presently working code goes against the maxim “If it’s not broken, don’t fix it.” ??

    Thread Starter thetoolman123

    (@thetoolman123)

    Cool, thanks for that. Yes it all works. I cheated a bit and asked Bard for the code! I’ve updated it to use the:

    $args = [
      'category_name' => 'interviews',
      'posts_per_page' => 1,
    ];

    and that works perfectly! Thanks

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Displaying 2 features posts’ is closed to new replies.