WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Sticky posts doesn't work on query_posts? (22 posts)

  1. akis
    Member
    Posted 4 years ago #

    I have the following code:

    <?php
    $sixt_id = get_catId("Blog");
    $sixt_id .= ",".get_catId("Featured");
    $sixt_id .= ",".get_catId("Premium");
    
    query_posts("showposts=6&cat=".$sixt_id);
    if (have_posts()) : while (have_posts()) : the_post();
    
    echo get_the_title();
    
    endwhile;
    endif;
    ?>

    On featured category I have a sticky post, but doesn't show up in front, why?

  2. Mark / t31os
    Moderator
    Posted 4 years ago #

  3. akis
    Member
    Posted 4 years ago #

    Thanks. I have checked those but didn't help, I don't want to display only sticky posts, I just want the normal behavior.

  4. Mark / t31os
    Moderator
    Posted 4 years ago #

    Stickies should already be included in the query, unless they're not matched against the parameters you've set, ie. within the categories above (blog, featured, premium).

  5. akis
    Member
    Posted 4 years ago #

    They are included but like normal posts not stickies.

  6. Mark / t31os
    Moderator
    Posted 4 years ago #

    Try setting the caller_get_posts parameter (try both values, 1 and 0, should only take a minute).

  7. akis
    Member
    Posted 4 years ago #

    Tried that already, nothing changed.

  8. Mark / t31os
    Moderator
    Posted 4 years ago #

    I have no idea why they're not retaining the order, if you remove the query posts line do stickies then behave correctly? (simply to test).

  9. akis
    Member
    Posted 4 years ago #

    Yes, but unfortunately this is not a solution.

    Some related question:
    Do you have any idea if its possible to prioritize posts from Featured category?

  10. Mark / t31os
    Moderator
    Posted 4 years ago #

    Was never intended to be a solution, simply a test to see if the problem is still apparent.

    Query posts should work with stickies if caller_get_posts is set accordingly, but i do remember a thread from a while back that i answered where another user had the same problem, i'll see if i can dig it up.. (will edit post if i can find it).

    EDIT: Here's the thread i mentioned which contains a work-around for the problem.
    http://wordpress.org/support/topic/309252

    Not sure what you mean on your related question, can you elaborate please?

  11. akis
    Member
    Posted 4 years ago #

    My code displays last six posts from those 3 categories.
    If possible, I want to show posts from Featured category first. Featured may contain posts from Blog and other categories.

  12. akis
    Member
    Posted 4 years ago #

    It seems that using this query, sticky posts are displayed fine but the showposts count no longer works, it displays 6 normal posts + x sticky posts. Do you have any ideea why?

    <?php
    query_posts("showposts=6&".array('category__in' => array($sixt_id)));
    ?>
  13. Mark / t31os
    Moderator
    Posted 4 years ago #

    You can't combine a string and an array, either use an array or a string for the parameters.

    Try this..

    query_posts( array( 'posts_per_page' => 6, 'category__in' => array( $sixt_id ) ) );

    NOTE: Changed to posts_per_page as showposts is deprecated, however showposts does still work(currently), simply showing you the new way... :)

  14. akis
    Member
    Posted 4 years ago #

    For some reason now it displays posts only from the first category (Blog).

  15. Mark / t31os
    Moderator
    Posted 4 years ago #

    You variable is not an array, it's a string.. (i missed that before).

    Change this.

    $sixt_id = get_catId("Blog");
    $sixt_id .= ",".get_catId("Featured");
    $sixt_id .= ",".get_catId("Premium");

    to...

    $sixt_id = array( get_catId("Blog"), get_catId("Featured"), get_catId("Premium") );

    ..and as the variable is now an array, change this..

    query_posts( array( 'posts_per_page' => 6, 'category__in' => array( $sixt_id ) ) );

    to..

    query_posts( array( 'posts_per_page' => 6, 'category__in' => $sixt_id ) );

    Or, just do this..

    query_posts( array( 'posts_per_page' => 6, 'category__in' => array( get_catId("Blog"), get_catId("Featured"), get_catId("Premium") ) ) );
  16. akis
    Member
    Posted 4 years ago #

    Sorry for the delay, still doesn't work.
    Using an array for posts_per_page seems to disable the sticky behaviour.

    What I really want:
    - I'm showing six posts from Blog category on homepage (ordered by date)
    - Here, I want to promote (give priority) to some old posts (from the Blog category)

    Any ideea how to achieve this?

  17. Mark / t31os
    Moderator
    Posted 4 years ago #

    Get rid of the query_posts line and replace it with a filter to choose the category..

    function home_query( $query ) {
    	if( $query->is_home ) {
    		$query->set( 'category__in' , array( 1, 2, 3 ) );
    		//$query->set( 'caller_get_posts' , 1 );
    	}
    	return $query;
    }
    add_filter( 'pre_get_posts' , 'home_query' );

    Might not need the caller_get_posts parameter, can uncomment if necessary though. 1, 2 and 3 are example category IDs, replace them with the required category ID(s).

    Functions file of your theme would be a suitable place to put the code.

  18. akis
    Member
    Posted 4 years ago #

    Thanks.
    I forgot to say that this is just a small part of homepage (multiple query_posts, etc). Anyway, it seems to work well but for some reason posts are limited to 5.

    Added
    $query->set( 'posts_per_page' , 6 );
    Now it displays 6+x sticky posts. I don't really understand why it counts sticky posts separately.

  19. Mark / t31os
    Moderator
    Posted 4 years ago #

    You can set that inside the filter to if you like.

    function home_query( $query ) {
    	if( $query->is_home ) {
    		$query->set( 'category__in' , array( 1, 2, 3 ) );
    		$query->set( 'posts_per_page' , 20 );
    		//$query->set( 'caller_get_posts' , 1 );
    	}
    	return $query;
    }
    add_filter( 'pre_get_posts' , 'home_query' );

    Adjust 20 to the desired amount.

  20. akis
    Member
    Posted 4 years ago #

    Hah, I've just added a $i count in while and break after six.
    Kinda ugly but seems to work.

    Thank you so much for your help, t31os.

  21. Mark / t31os
    Moderator
    Posted 4 years ago #

    Sticky posts are persistent content, they stay at the top and they appear regardless of how many posts are set to appear, they're "stuck" in a manner of speaking, i think that's the idea anyway.

    You're welcome in any case.. :)

  22. carl-johan
    Member
    Posted 3 years ago #

    Mark - you don't think there's any chance to get this working with pagination? To me, the sticky posts appear on top (very nice solution btw cheers) but they are not "counted" as parts of the pagination. What I'm trying to say is this, I have set posts_per_page to 3 but on my first page in the pagination I get 3 plus my sticky one.

    Either way, cheers for a nice solution!

Topic Closed

This topic has been closed to new replies.

About this Topic