Blog layout: related post based on custom taxonomy: re-opened

Hi
I re-opened this topic it was closed just before I could answer it, sorry!

I think This code is the same I used and doesn’t work.

Take this post as example: https://cueilleurs-sauvages.ch/staging/blog/sapin-blanc-abies-alba-le-reconnaitre-et-cuisiner/

It’s cutom post taxonomy term is “sapin blanc”. Click on it to see the post it contains.

If I put this code in the “query string” it should shows post of this term “sapin blanc”, but it shows nothing:

Thanks for your help!

Hello @Cormoran42,

You will need to have this structure:

- DIV -> this is your Looper Provider Current Terms  to get the current terms with Looper Consumer set to 1
    -  Posts - Looper Provider Query String which will contain our query
        - Post - Looper Consumer to display the related post items

Query Args:

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

$query = array(
   'post_type'           => 'post',
   'post_status'         => 'publish',
   'ignore_sticky_posts' => 1,
'post__not_in'        => array( '{{dc:post:id}}' ),
   'posts_per_page'      => 5,
   'orderby'             => 'title',
   'order'               => 'desc',
   'tax_query'           => $tax_query
);

var_dump( http_build_query( $query ) );

We need to include 'post__not_in' => array( '{{dc:post:id}}' ) so that the current post item will not be included. So the final query string will be:
post_type=post&post_status=publish&post__not_in%5B0%5D={{dc:post:id}}&ignore_sticky_posts=1&posts_per_page=5&orderby=title&order=desc&tax_query%5B0%5D%5Btax_query%5D%5B0%5D%5Btaxonomy%5D=plantes_sauvages&tax_query%5B0%5D%5Btax_query%5D%5B0%5D%5Bfield%5D=slug&tax_query%5B0%5D%5Btax_query%5D%5B0%5D%5Bterms%5D={{dc:term:slug}}&tax_query%5B0%5D%5Btax_query%5D%5B0%5D%5Boperator%5D=IN

Kindly let us know how it goes.

Hi
I appreciate your help. I checked the structure and it was the same as your’s. I updated the query string but it still shows nothing.

thanks for your help.
michael

Hello Michael,

On this post item, https://cueilleurs-sauvages.ch/staging/blog/sapin-blanc-abies-alba-le-reconnaitre-et-cuisiner/, the “plantes_sauvages” is “sapin-blanc”. At the bottom of the post, it is displaying 5 “sapin-blanc” items:

You can view all 6 “sapin-blanc” post items in the archive here:

Please check. the post item again.

Hi
Thanks for your help!

You are right, the code is working, but only on this particular post. Check this:

  • This post has only one custom term “sapin blanc”: the code works!

  • This other post has 2 terms “sapin blanc + if” = the code shows only one post thas has both terms, i’d like it to show posts that has “If” or “sapin blanc” terms

  • This other post has many terms: the code shows nothing.

In conclusion i’d like the code to show a list of post containing any of the terms, not all. Is it an operator setting?

thanks!

Hi Michael,

I have checked the This other post and found that the DIV element is set to the Looper Provider with Taxonomy and Looper Consumer to One. That is why it shows only one Taxonomy, you need to set it All. If you want to show a single post from each taxonomy you need to set One to the Post element where the Looper Consumer is set to All.

Screenshot 2023-02-20 174541

And also the same thing happens to other posts too. I would suggest you check the Looper Provider for each case on this.

Thanks

Ok thanks, I begin to understand better how it works, I’m almost there.

But when I set both looper consumer (DIV and Post) to ALL, it will display 5 posts by taxonomy. So it’s ok if there’s only one taxonomy.

But now, the posts that have a lot of terms will display too much posts (5x5= 25 if I understand well)

I’d like 5 posts max on total. And I don’t want to put Post Lopper consumer to one, since I want 5 post also if there’s only one taxonomy.

Tell me if it 's not clear :sweat_smile:

Hi Michael,

I understand what you are trying to do here. But unfortunately, there is no such way to acheive that, except customization. You can use the Custom Looper to get the actual array of posts which can be shown on the page. I would suggest you go through the following article and thread which may help you with this.



If you are not proficient in this, I would suggest you hire a developer who can assist you to do the customization or you can avail of our newly launched service called One, where we answer the questions beyond normal theme support.

Thanks

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