How to display sticky or latest published post on the current category page it belongs to?

What I'm able to do :

When there is a sticky post in the current category, I'm already able to display it. The problem occurs when there is no sticky post : it doesn't display the latest post as it should do (actually id doesn't display anything).

It works like a charm on the Home page : when there is a sticky post, it displays it, and where there is no sticky post, it displays the latest post.

Here is my code :

?php
$current_object = get_queried_object_id(); // Get the category ID and store it in a variable

    // Display sticky post or latest post :
    $args = array(
      'posts_per_page'      = 1,
      'post__in'            = get_option( 'sticky_posts' ),
      'cat'                 = $current_object, // Current category ID 
      'ignore_sticky_posts' = true,
    );
    $featured_query = new WP_query( $args );

    if ( $featured_query-have_posts() ) : ?

      div class=featured-post

        ?php
        while ( $featured_query-have_posts() ) :
          $featured_query-the_post();

          the_title('h2', '/h2');
          the_excerpt();

        endwhile;
        wp_reset_postdata();
        ?

      /div

    ?php endif; ?

What I want to achieve :

I want my category page to display the sticky post that belongs to this category if it exists, and if not, I want that the category page displays the latest post published in this category.

What am I missing ?

Topic sticky-post categories Wordpress

Category Web


This code is certainly not perfect but it actually does the job :

<?php
// Store the current category ID :
$current_object = get_queried_object_id();

// Get sticky post :
$args = array(
  'posts_per_page'      => 1,
  'post__in'            => get_option( 'sticky_posts' ),
  'cat'                 => $current_object,
  'ignore_sticky_posts' => true,
);
$sticky_query = new WP_query( $args );

// Get latest post :
$args = array(
  'posts_per_page'      => 1,
  'cat'                 => $current_object,
  'ignore_sticky_posts' => true,
);
$latest_query = new WP_query( $args );

// If there is a sticky post in the current category :
if ( $sticky_query->have_posts() ) : ?>

  <div class="sticky-post">

    <?php
    while ( $sticky_query->have_posts() ) :
      $sticky_query->the_post();

      the_title();

    endwhile;
    wp_reset_postdata();
    ?>

  </div>

<?php
// If there is no sticky post, display the latest published post :
elseif ( $latest_query->have_posts() ) : ?>

  <div class="latest-post">

    <?php
    while ( $latest_query->have_posts() ) :
      $latest_query->the_post();
      
      the_title();

    endwhile;
    wp_reset_postdata();
    ?>

  </div>

<?php endif; ?>

There is two queries here. The first one to get and display the sticky post that belongs to the current category, the second one to get and display the latest post in the same category (if there is no sticky post to display).

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.