• Im showing posts from specific category on my home page.
    2 categories to be exact.

    But when sticky posts are not sticky when i run this query meaning the posts are organized by date.

    I want sticky posts to be sticky what am i missing in this query?

    <?php
    $args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'category_name' => 'category1,category2',
        'posts_per_page' => -1,
    	'ignore_sticky_posts' => -1
    );
    $arr_posts = new WP_Query( $args );
     
    if ( $arr_posts->have_posts() ) :
     
        while ( $arr_posts->have_posts() ) :
            $arr_posts->the_post();
    ?>
    
    <?php
    get_template_part( 'template-parts/content', esc_attr( treville_get_option( 'blog_layout' ) ) );
    ?>
    
    <?php
    endwhile;
    endif;
    ?>
    • This topic was modified 3 years, 8 months ago by Gustav.
Viewing 6 replies - 1 through 6 (of 6 total)
  • The ‘ignore_sticky_posts’ parameter is a boolean. By passing a -1, you have passed a truthy value, so sticky posts will be ignored.
    Be careful when you use ‘posts_per_page’ with -1, since you don’t know how many there actually are and could be overwhelmed with data, not to mention providing a way for hackers to slow down your site or use all your bandwidth.

    Thread Starter Gustav

    (@4ever16)

    @joyously i removed the line “‘ignore_sticky_posts’ => -1” didnt help.

    Is the template part checking for sticky posts?

    Thread Starter Gustav

    (@4ever16)

    Well i added this query in home.php and i get the issue that the sticky post is not at the top anymore.

    When i run default home.php without this query then the sticky post is at the top.

    When you look at the code, right here: https://core.trac.www.ads-software.com/browser/tags/5.7/src/wp-includes/class-wp-query.php#L3143
    you’ll see that sticky only applies to a home page query. Your query is for categories.

    Moderator bcworkz

    (@bcworkz)

    If your intention is to limit home page queries to specific categories, don’t make a new query, modify the existing one through the “pre_get_posts” action. You can ensure you’re altering the right query by checking if the query’s is_home property is true.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘WP query sticky is not sticky’ is closed to new replies.