【问题标题】:Filter custom post type by category in REST API v2 in WordPress在 WordPress 的 REST API v2 中按类别过滤自定义帖子类型
【发布时间】:2017-03-16 09:40:58
【问题描述】:

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

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`

【问题讨论】:

    标签: json wordpress wordpress-rest-api


    【解决方案1】:

    我的自定义帖子类型也遇到了这个问题。

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

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

    【讨论】:

      【解决方案2】:

      这节省了我自己的一天

      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;
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2019-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-17
        • 1970-01-01
        • 2021-06-07
        • 1970-01-01
        相关资源
        最近更新 更多