Site logo

Archived topic

Query Loop

10 replies · Started by Adarsha on July 3, 2022

Viewing posts 1–11 of 11

Hey, I am using query loop for related posts via elements.
But the post under which I use also shows the same post in related post.
Is there a option to disable showing the main post?

I was using WP show posts till now and it had a way to hide the main post.

Hi Adarsha,

To clarify, are you referring to excluding the current post in your Query Loop Block?

If so, you can use a filter to do as such.

Add my-class-name to the Post Template Block as such: https://share.getcloudapp.com/qGuOrgnZ

Then add this PHP snippet:

add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
    if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'my-class-name' ) !== false ) {
        
		$query_args['post__not_in'] = [get_the_ID()];
    }

    return $query_args;
}, 10, 2 );

Adding PHP reference: https://docs.generatepress.com/article/adding-php/#code-snippets

Hope this helps!

Snippets plugin has option to "run the php sitewide"
Should I do that?

Yes.

I'm not sure how to add the "my-class-name"

I am using elements for related posts.
So for each category, there is new element.

I tried adding it to the element and that does not hide.

That worked. Thank you

While I'm at this, can I deactivate snippets plugin? I have 2 php codes added through it. (Enabled in it)
Should I keep the plugin active all the time?

Yes, you'll need to keep the plugin activate all the time as long as you need the PHP snippet.

Alright, thank you :)

No problem :)

This archived topic is closed to new replies.