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.


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 );   
} );

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

1 Like

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?

Any updates yet about related posts for Cwicly being build in?

Hi @webmaat,

Have you given this a try?

1 Like

Thanks! That does a part of the job. Great! But I now see the problem is that Rankmath doesn’t create an option for primary category on a CPT so when that’s fixed, I really have what I want. I send them a ticket.

I can conform it works with Rankmath. top!

@webmaat, can you post your config for pulling the category info from RankMath? Is it simply the same as @Louis’s screenshot, from above?

yeah. get the correct category and set up Rankmath ‘advanced’ (only then primary categories work)