Excluding current post title

Hi,

I’m trying to achieve something on a layout template page assigned to a single post type (town) using looper/consumer.

The town post type contains several posts with every post’s title being the town name.

  • The goal: display a list of all the post titles (as buttons) on every town page excluding the current post

I was able to achieve the following:

  1. Created a provider with the Query Builder set to “Town” as post with a count of 20
  2. Created a button (consumer)
  3. Button’s URL was is set to {{dc:post:permalink}}
  4. Button’s name is set to {{dc:post:title}}

This brought me very close to what I wanted because it outputted all the buttons that had the name of each town and link to each town page. However, how would I exclude the current (viewing) town page from being displayed?

Thanks

Hi @kgpthemex,

I would suggest you use the Query String and add the post__not_in attribute into the Query String and mention the {{dc:post:id}} to exclude the current post from the post query. Your Query String would look like the following one.

post_type=town&posts_per_page=20&offset=0&ignore_sticky_posts=1&post__not_in={{dc:post:id}}

Hope it helps.
Thanks

@tristup,

Thank you for that. I did not know how to exclude something. Is there a list of attributes? Unfortunately, the code works up to this:

post_type=town&posts_per_page=20&offset=0&ignore_sticky_posts=1

Unfortunately, this part does not work:

&post__not_in={{dc:post:id}}

I tried both ({{dc:post:id}}) and ({{dc:post:title}})

I did a test on a looper with just {{dc:post:title}} to see if it can recognize the post I’m on but it looks like nothing pulls away.

Hello @kgpthemex,

Sorry for the confusion post__not_in requires an array of the integer values so I would suggest you use the post__not_in like this. &post__not_in%5B0%5D={{dc:post:id}}

If it doesn’t work for you, please share your details in a secure note. Please provide the following details

  • WordPress Login URL
  • Admin level username and password
  • Exact page URL

You can find the Secure Note button at the bottom of your posts

Thanks

@prakash_s

Thank you for getting back to me with a potential solution. Unfortunately, I don’t understand what this %5B0%5D portion refers to. What am I replacing this with?

Thanks again

Hello @kgpthemex,

It is used to represent the array in the query string. Your WordPress query to exclude the current post would be like this.

$args = array(
'post_type' => 'post',
'posts_per_page' => 20,
'post_status'=>'publish',
'offset'=>0,
'ignore_sticky_posts'=> -1,
'post__not_in'=> array(5)
);

By utilizing http_build_query() , which will format our string accordingly:

var_dump( http_build_query( $args ) );

We can have this:

 post_type=post&posts_per_page=20&post_status=publish&offset=0&ignore_sticky_posts=-1&post__not_in%5B0%5D=5

You just need to replace the post ID with the current post ID by dynamic content code.

 post_type=post&posts_per_page=20&post_status=publish&offset=0&ignore_sticky_posts=-1&post__not_in%5B0%5D={{dc:post:id}}

Please have a look at this explanation of @Kory on this thread (Looper Query String)

Hope it helps
Thanks

@prakash_s,

Thank you for the detailed explanation and for the reference. I tried it as it is and worked great.

I appreciate the support team!

You are most welcome, @kgpthemex.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.