• Resolved jneug

    (@jneug)


    I want to use the Query Loop block to show sticky posts at the top of my start page. It works as long as there are some sticky posts, but otherwise it seems to show all posts. Is this the intended behavior? I expected it to be empty in that case.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support ying

    (@yingscarlett)

    Hi @jneug ,

    GB’s query loop follows the same behaviour as the WP query loop.

    So yes, it’s the intended behaviour.

    However, if you want to change its behaviour when there’s no sticky posts found in the loop, try the steps below:

    1. Add an additional CSS class to the Grid block nested directly in the Query loop block, eg. sticky-post-loop.
    2. Add this PHP code:
    add_filter( 'render_block', function( $block_content, $block ) {
        if ( ! empty( $block['attrs']['className'] ) && 'sticky-post-loop' ===  $block['attrs']['className']  )  {
            $sticky_posts = get_option( 'sticky_posts' );
            if ( empty( $sticky_posts ) ) {
                $block_content = '';
            }
            return $block_content;  
        }
    
        return $block_content;
    }, 10, 2 );
    
    • This reply was modified 3 years, 2 months ago by ying.
    Thread Starter jneug

    (@jneug)

    Thanks. I fixed it by modifying the query loop args like so:

    add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
        if ( isset($query_args['ignore_sticky_posts']) && $query_args['ignore_sticky_posts'] == 1 && count($query_args['post__in']) == 0 ) {
            $query_args['post__in'] = array(0);
        }
    
        return $query_args;
    }, 10, 2 );
    Plugin Support ying

    (@yingscarlett)

    Great! 🙂

    Hi there! We haven’t heard back from you for a while now so we’re going to go ahead and set this topic as resolved. Feel free to reply if you need any more help.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Query Loop sticky only setting’ is closed to new replies.