Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] REST API endpoint for pulling posts from primary category #6251

Open
ChrisFlannagan opened this issue Dec 13, 2016 · 2 comments

Comments

@ChrisFlannagan
Copy link

Looked for a function or api method to pull posts that have a specific primary category

I wrote up a blog article on how I pulled this off and Joost asked me to open an issue. My solution works, it's slightly custom to my specific needs by pulling in children as well, but seems to do the trick and easy to adjust.

https://whoischris.com/yoast-primary-category-endpoint-wp-rest-api/

https://gist.github.com/ChrisFlannagan/a6f63a02ea16268a25bc5d386e9ac63a#file-primary-cat-php

@CarolineGeven
Copy link
Contributor

Thanks for requesting this enhancement.

Unfortunately we're not able to implement this at short notice. Currently we're focusing on issues that affect many users. We've concluded that this enhancement is not needed by many users, therefore we are not able to implement this short-term. However, if more users are in need of this, we'll of course revisit this issue.

@kopepasah
Copy link

Hello Yoast team! 👋🏻

For a recent project, I needed a way to request posts from the REST API matching the primary category of a particular post. I discovered this Issue and original solution provided by Chris above. However, I was able to query posts from the Primary Category by filtering the rest_post_query and adding a meta query for _yoast_wpseo_primary_category.

<?php
add_filter(
	'rest_post_query',
	function ( $args, $request ) {
		if ( ! empty( $request['primary_category'] ) ) {
			$args['meta_query'] = [
				[
					'key'     => '_yoast_wpseo_primary_category',
					'value'   => $request['primary_category'],
					'compare' => '=',
				],
			];
		}

		return $args;
	},
	10,
	2
);

While I personally do not feel this needs to belong in Yoast WordPress-SEO plugin, if is added to the plugin, it should be extended in a different manner to match the ability to query from different taxonomies based on the WPSEO_Primary_Term class. Probably also want to use a different parameter name and add some checks for extending meta query if it exists (instead of overriding).

Happy to submit a patch, if Yoast SEO team determines this is acceptable functionality for the plugin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants