Blog layout: related post based on custom taxonomy

hi!
I’m creating a blog layout and want to add related posts section, based on a custom taxonomy (plantes_sauvages) . I’v read tons of topics and tried tons of queries, but noting is working.

The code I have is this, but the post shown are not related to the custom taxonomy:

$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,
   'posts_per_page'      => 5,
   'orderby'             => 'title',
   'order'               => 'desc',
   'tax_query'           => $tax_query
);

var_dump( http_build_query( $query ) );

Thanks for your help!

Michaël

Hello Michaël,

Thanks for writing to us.

In order to help you with your concerns we need to check your settings I would request you, please share the admin login details meanwhile I would suggest you troubleshoot a few of the common issues before we investigate your settings. Please share your details in a secure note. Please provide the following details

  • WordPress Login URL
  • Admin-level username and password

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

Thanks

Hi!
thanks for your help! This would be the related post section, at the end of the blog layout.

In your code whenever your doing something custom you are going to have to call dynamic content yourself. See the below code. You are going to use that function on any string you need to expand. Let us know if you have any questions or if that was not what you are looking for.

cs_dynamic_content("{{dc:term:slug}}"),

Hi !
You mean like this? This create critical error. I’m not sure where to put that function in my code.

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

Thanks !

Hey @Cormoran42,

Please remove this in your child theme’s functions.php file so that we can load the builder.

$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,
   'posts_per_page'      => 5,
   'orderby'             => 'title',
   'order'               => 'desc',
   'tax_query'           => $tax_query
);

var_dump( http_build_query( $query ) );

And then in your query string, you can use this:
post_type=post&post_status=publish&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

Hope this helps.

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