在 WordPress 的 REST API v2 中按类别过滤自定义 post 类型

Filter custom post type by category in REST API v2 in WordPress

我正在使用 WP API 创建一个 Android 应用程序。我设法在 REST API 中显示自定义 post 类型,可以在以下位置查看:

http://localhost/wordpress/wp-json/wp/v2/property

现在我想按类别过滤属性,例如villahomerent 等。我尝试了以下方法,但它不起作用:

http://localhost/wordpress/wp-json/wp/v2/property?filter[category_name]=villa` 
http://localhost/wordpress/wp-json/wp/v2/property?filter[category]=apartment`

我 运行 也遇到过这个问题以及我的自定义 post 类型。

您需要根据您的自定义分类法(例如 property_categories 或您在 register_taxonomy() 函数中命名的任何名称)进行查询,然后然后按期限。

&filter[taxonomy]=property_categories&filter[term]=villa

这拯救了我自己的一天

mysite.com/wp-json/wp/v2/property?property-status=33

那么这是我在主题函数文件中的调用

    //get custom post types in the WP-API v2
//for mobile app
add_filter( 'register_post_type_args', 'sb_add_cpts_to_api', 10, 2 );
function sb_add_cpts_to_api( $args, $post_type ) {
if ( 'property' === $post_type ) {
$args['show_in_rest'] = true;
}
return $args;
}


//to add taxonomy for properties to API result
    add_action( 'init', 'sb_add_taxes_to_api', 30 );
    function sb_add_taxes_to_api() {
    $taxonomies = get_taxonomies( 'property-city', 'property-type', 'property-status' );
      foreach( $taxonomies as $taxonomy ) {
            $taxonomy->show_in_rest = true;
            $taxonomy->name;
        }
    }