Question

I can pull the terms from a post to return only the Primary Category that Yoast SEO plugin provides.

But what I'd like to do is the following:

Query latest 1 post from category "x". But, the post returned has to have that category set as the Primary. It can't return the latest if that post has not marked that cat as primary.

Was it helpful?

Solution

Yoast SEO stores the primary term ID as the post meta field _yoast_wpseo_primary_{taxonomy}. Knowing this you can use a meta query to get all posts with primary category ID X:

$query = new WP_Query([
    'cat' => 'X', // Just in case Yoast data corrupted & post no longer attached to X term but primary meta remains
    'meta_query' => [
        [
            'key' => '_yoast_wpseo_primary_category',
            'value' => 'X',
        ]
    ],
]);
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top