0

How to stick multiple posts in a single category to show on top of that category?

Tried Ideas: I tried to use "Category Sticky Post" plugin, but it only allows me to pin one post for each category.

Also tried to stick posts to the homepage, and remove the featured posts from the home page, but they don't stick on top of the category.

  • What have you already attempted? – Max Yudin May 22 '17 at 11:46
  • "Category Sticky Post" plugin, but it only allows me to pin one post for each category. Also tried to stick posts to homepage, and remove the featured posts from the home page, but they don't stick on top of the category. – Mohammed Anghabo May 22 '17 at 11:54
  • Most simple method is to change date of publication and sort posts in category how you need.. – David Klhufek May 22 '17 at 13:36
  • true .. but I need to change the time each time I -or any other editor- add a new post ... which isn't that much practical !! but thanx tho !! – Mohammed Anghabo May 22 '17 at 18:34
0

Your category.php should have two loops — one for stickies, one for regular posts:

<?php
// get sticky posts array
$sticky = get_option('sticky_posts');

// First WP_Query arguments
$args = array(
    // include stickies
    'post__in'  => $sticky
);

$query = new WP_Query( $args );

// First loop for stickies only
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post() );

    echo get_the_title() . '<br />';
    echo get_the_content() . '<br />';

endwhile; endif;

wp_reset_postdata(); // don't forget to reset before the next loop

/***************************************/

// Second WP_Query arguments
$args = array(
    // exclude stickies
    'post__not_in' => $sticky
);

$query = new WP_Query( $args );

// Second loop for posts excluding stickies
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post() );

    echo get_the_title() . '<br />';
    echo get_the_content() . '<br />';

endwhile; endif;

wp_reset_postdata(); // if necessary

More than likely, you will need a lot of additional WP_Query parameters.

  • working great .. had to add few parameters ... but managed to work it out – Mohammed Anghabo May 23 '17 at 7:28

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.