Query other posts with duplicate category

Another query question here… how can I query all posts that are assigned to the same primary category as the current post (for a related content slider)?

That’s a good one @avunu,

We might have to add a small dynamic property to retrieve the current item’s category/taxonomy.

2 Likes

that would be great!

Maybe having the “shortcode” option in the dynamic field selection could also help with more personalized use cases. (basically to leave a door open to pass some other dynamic values into the query)

i meet the same requirement now(query posts to it’s same category or tag), showing related posts is common function which people use.

kindly reminder, seems can not find this “small dynamic property” at the moment.

I’m using the shortcode option with the following code to return the current posts category id:

add_shortcode( 'category_id', function() {
	global $post;
	$postcat = get_the_category( $post->ID );

	if ( ! empty( $postcat ) ) {
		return esc_html( $postcat[0]->term_id );   
	}
	return;
} );

You can just paste this into a snippet with the Code Snippets plugin, and then set up your query like so:

thanks so much, i will try. really appreciate.

if @Louis can put “dynamic property”, that will be perfect for other users like me who do not know dynamic coding.

Please, clarify! Could you define “primary”.
Basically, I don’t know anything about “primary category” (kind of favorite) in Wordpress and the category that you query on is the “first” one that your arg will select. (alphabetic order?) and not the “primary”.

Does this help? How to select a primary category • Yoast

Yes, you need an extension to set a primary category. That confirms my previous post. That is not native Wordpress.
No need any extension, you can also easily do the same with ACF.

Dear, boss, does this add in the schedule?