Yoast / wordpress-seo

Yoast SEO for WordPress
https://yoast.com/wordpress/plugins/seo/
Other
1.77k stars 892 forks source link

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

Open ChrisFlannagan opened 7 years ago

ChrisFlannagan commented 7 years ago

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 commented 7 years ago

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 commented 1 year ago

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.