Looks like you can use the yoast_get_primary_term( $taxonomy, $post_id )
function to get the name of the primary term for the post. You could use that in a weighting function to adjust the post weight if the search term matches the primary term name.
However, since yoast_get_primary_term()
uses get_post()
to get the post and get_term()
to get the primary term, that’s potentially expensive and on a large site may lead to hundreds of extra database calls per search.
The best approach would be to construct a single database call that gets the (post ID, primary term name) pairs from the database, run that first and store the results in an array to use later. That would be more efficient. I’m not sure how Yoast SEO stores the data. If it’s still using the _yoast_wpseo_primary_category
custom field, building the MySQL query to get the post IDs and term names shouldn’t be difficult.