Query string for the same term in a custom taxonomy

Hi!

I built a CPT (actividad) which has a custom taxonomy attached (localizacion). I have a loop provider set up in a row in the Actividad layout, but I want it to only display the posts that have the same “localizacion” value as the current post. How can I built that query string?

I tried to customize an answer given by @friech some days ago (Can a range of posts be displayed depending the current post being viewed?) with this result:

post_type%5B0%5D=actividad&posts_per_page=6&ignore_sticky_posts=1&tax_query%5B0%5D%5Btaxonomy%5D=localizacion&tax_query%5B0%5D%5Bterms%5D={{dc:term:slug}}&tax_query%5B0%5D%5Bfield%5D=slug&tax_query%5B0%5D%5Boperator%5D=IN&tax_query%5B0%5D%5Binclude_children%5D=0&post__not_in%5B0%5D={{dc:post:id}}

but I keep getting an empty row. Any help?

Thanks, Sergio

1 Like

Hello Sergio,

Thanks for writing to us.

It seems that the code is working fine at my end in order to help you with your concern we need to check your settings. 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

Secure note added, thanks a lot,
Sergio

Hi Sergio,

It seems that the Query String is having the formatting issue, I would suggest you go through the following thread describing the Taxonomy query format and how to convert it as Query String.


Hope it helps.
Thanks

I’m afraid I cannot get it to work… I just got

post_type=actividad&post_status=publish&ignore_sticky_posts=1&posts_per_page=3&orderby=rand&order=asc&tax_query%5B0%5D%5Btaxonomy%5D=localizacion&tax_query%5B0%5D%5Bfield%5D=slug&tax_query%5B0%5D%5Bterms%5D={{dc:term:slug}}&tax_query%5B0%5D%5Boperator%5D=IN

as the query string, but it won’t work. Any ideas?

Hi Sergio,

I have created the Query and generated the Query String as described in those reference threads. Please find the Query and the Query String below.

The Query:

$tax_query[] = array(
    'taxonomy' => 'localizacion',
    'field'    => 'slug',
    'terms'    => {{dc:term:slug}},
    'operator' => 'IN',
);

$query = array(
    'post_type'           => 'actividad',
    'post_status'         => 'publish',
    'ignore_sticky_posts' => 1,
    'posts_per_page'      => 6,
    'orderby'             => 'rand',
    'order'               => 'asc',
    'post__not_in'        => {{dc:post:id}},
    'tax_query'           => $tax_query
);

The Query String:

post_type=actividad&post_status=publish&ignore_sticky_posts=1&posts_per_page=6&orderby=rand&order=asc&post__not_in={{dc:post:id}}&tax_query%5B0%5D%5Btaxonomy%5D=localizacion&tax_query%5B0%5D%5Bfield%5D=slug&tax_query%5B0%5D%5Bterms%5D={{dc:term:slug}}&tax_query%5B0%5D%5Boperator%5D=IN

I would suggest you use the following Query String instead of the above one and check if that works for you.

localizacion={{dc:term:name}}&post_type=actividad&posts_per_page=3&offset=0&ignore_sticky_posts=1&post__not_in%5B0%5D={{dc:post:id}}

Thanks

Hi again
The second query string gets three posts from actividad, but they are not restricted to those sharing the same value in the localizacion taxonomy.
I also tried the first one, and it returns nothing.

Best regards,
Sergio

Hello Sergio,

Kindly check out this old thread:

In your case, the query string can be:
taxonomy={{dc:term:slug}}&post_type=actividad&posts_per_page=3&offset=0&ignore_sticky_posts=1&post__not_in%5B0%5D={{dc:post:id}}

Kindly let us know if this works out for you.

Hi!
I’m afraid it doesn’t work either. Mind that I have two custom taxonomies defined for that CPT, and I think (correct me if I’m wrong) that {{dc:term:slug}} could refer to either one of those taxonomies.
Now I realize I didn’t bring up the two taxonomies thing earlier, sorry if that makes a difference in approach. I only want one of those (localizacion) to match the current post.

Thanks again,
Sergio

Hi again
In any case, I’m going to solve this problem with a relationship on the ACF fields of the CPT. Anyway, it would be great to find a solution to find a way of using the custom taxonomy.

Best regards, Sergio

Hi Sergio,

I have checked it again and found that the {{dc:term:slug}} or {{dc:term:name}} is not showing the value and i.e. why the Query String is not able to find the localizacion for the specific Term.
I would suggest you update the Pro to its latest version 5.0.8, and check that if the Query String works.

Thanks

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