WP REST API从POST类型获取帖子

时间:2015-09-04 作者:Jeff

如何使用WP restapi(v1或v2)从特定的定制帖子类型获取所有帖子?我对这一点很陌生,正在努力了解如何做到这一点。

我目前正在使用WP-restapi v2,并使用此工具获取了所有帖子类型的列表

http://domain.com/wp-json/wp/v2/types
然后找到了我感兴趣的帖子类型

http://domain.com/wp-json/wp/v2/types/the-icons-update
如何从该特定内容类型获取所有帖子?

我试过了

http://domain.com/wp-json/wp/v2/posts?filter[post_type]=the-icons-update
但它返回一个空数组(我想它返回默认帖子,在我的站点上,只有我试图检索的自定义帖子类型中的帖子)。

我如何注册帖子类型会有问题吗?

function custom_post_type() {
$labels = array(
    \'name\'               => _x( \'The Icons Update\', \'post type general name\' ),
    \'singular_name\'      => _x( \'The Icons Update\', \'post type singular name\' ),
    \'add_new\'            => _x( \'Add Page\', \'magazine\' ),
    \'add_new_item\'       => __( \'Add New Page\' ),
    \'edit_item\'          => __( \'Edit Page\' ),
    \'new_item\'           => __( \'New Page\' ),
    \'all_items\'          => __( \'All Pages\' ),
    \'view_item\'          => __( \'View Page\' ),
    \'search_items\'       => __( \'Search Pages\' ),
    \'not_found\'          => __( \'No Page found\' ),
    \'not_found_in_trash\' => __( \'No Page found in the Trash\' ), 
    \'parent_item_colon\'  => \'\',
    \'menu_icon\'          => \'\',
    \'menu_name\'          => \'The Icons Update\'
);
$args = array(
    \'labels\'        => $labels,
    \'description\'   => \'Holds our projects and project specific data\',
    \'public\'        => true,
    \'menu_position\' => 5,
    \'supports\'      => array( \'title\', \'editor\', \'thumbnail\', \'excerpt\', \'custom-fields\' ),
    \'has_archive\'   => true,
    \'taxonomies\'    => array(\'post_tag\', \'category\'),
    \'hierarchical\'  => false,
    \'query_var\'     => true,
    \'queryable\' => true,
        \'searchable\'    => true,
    \'rewrite\'       => array( \'slug\' => \'the-icons-update\' )
);
register_post_type( \'magazine\', $args );
flush_rewrite_rules();
}
add_action( \'init\', \'custom_post_type\' );
非常感谢您的帮助。

4 个回复
SO网友:Dioni Mercado

只需将下一个parmater添加到函数register\\u post\\u type中,它可以位于“menu\\u position”参数之前show\\u in\\u rest“=>true

enter image description here

如果您正在使用插件注册posttype,则可以使用以下代码:

add_action( \'init\', \'add_anuncios_to_json_api\', 30 );
function add_anuncios_to_json_api(){
    global $wp_post_types;
    $wp_post_types[\'anuncio\']->show_in_rest = true;
}
之后,您将能够列出mydomain中的帖子。com/wp json/wp/v2/posttype\\u slug

在我的例子中:mydomain。com/wp-json/wp/v2/anuncio

您还可以使用以下代码注册新的基址:

add_action( \'init\', \'add_anuncios_to_json_api\', 30 );
function add_anuncios_to_json_api(){
    global $wp_post_types;
    $wp_post_types[\'anuncio\']->show_in_rest = true;
    $wp_post_types[\'anuncio\']->rest_base = \'clasi\';
    $wp_post_types[\'anuncio\']->rest_controller_class = \'WP_REST_Posts_Controller\';
}
只需更换anuncio 对于你的帖子来说,slug和“clasi”将是你的路线。mydomain。com/wp json/wp/v2/clasi

SO网友:kabisote

要在版本2中显示自定义帖子类型,需要添加\'show_in_rest\' => true 在register\\u post\\u type函数参数中,具有该自定义帖子类型的帖子将在端点处可用:wp-json/wp/v2/your-custom-post-type。

资料来源:http://scottbolinger.com/custom-post-types-wp-api-v2/

SO网友:dev

好的,下面是我的完整答案:-

function prefix_register_post_type()
{
  register_post_type(
    \'prefix_portfolio\',
    array(
      \'labels\'        => array(
        \'name\'               => __(\'Portfolio\', \'text_domain\'),
        \'singular_name\'      => __(\'Portfolio\', \'text_domain\'),
        \'menu_name\'          => __(\'Portfolio\', \'text_domain\'),
        \'name_admin_bar\'     => __(\'Portfolio Item\', \'text_domain\'),
        \'all_items\'          => __(\'All Items\', \'text_domain\'),
        \'add_new\'            => _x(\'Add New\', \'prefix_portfolio\', \'text_domain\'),
        \'add_new_item\'       => __(\'Add New Item\', \'text_domain\'),
        \'edit_item\'          => __(\'Edit Item\', \'text_domain\'),
        \'new_item\'           => __(\'New Item\', \'text_domain\'),
        \'view_item\'          => __(\'View Item\', \'text_domain\'),
        \'search_items\'       => __(\'Search Items\', \'text_domain\'),
        \'not_found\'          => __(\'No items found.\', \'text_domain\'),
        \'not_found_in_trash\' => __(\'No items found in Trash.\', \'text_domain\'),
        \'parent_item_colon\'  => __(\'Parent Items:\', \'text_domain\'),
      ),
      \'public\'        => true,
      \'menu_position\' => 5,
      \'supports\'      => array(
        \'title\',
        \'editor\',
        \'thumbnail\',
        \'excerpt\',
        \'custom-fields\',
      ),
      \'taxonomies\'    => array(
        \'prefix_portfolio_categories\',
      ),
      \'has_archive\'   => true,
      \'rewrite\'       => array(
        \'slug\' => \'portfolio\',
      ),
    )
  );
}

add_action(\'init\', \'prefix_register_post_type\');


function prefix_register_taxonomy()
{
  register_taxonomy(
    \'prefix_portfolio_categories\',
    array(
      \'prefix_portfolio\',
    ),
    array(
      \'labels\'            => array(
        \'name\'              => _x(\'Categories\', \'prefix_portfolio\', \'text_domain\'),
        \'singular_name\'     => _x(\'Category\', \'prefix_portfolio\', \'text_domain\'),
        \'menu_name\'         => __(\'Categories\', \'text_domain\'),
        \'all_items\'         => __(\'All Categories\', \'text_domain\'),
        \'edit_item\'         => __(\'Edit Category\', \'text_domain\'),
        \'view_item\'         => __(\'View Category\', \'text_domain\'),
        \'update_item\'       => __(\'Update Category\', \'text_domain\'),
        \'add_new_item\'      => __(\'Add New Category\', \'text_domain\'),
        \'new_item_name\'     => __(\'New Category Name\', \'text_domain\'),
        \'parent_item\'       => __(\'Parent Category\', \'text_domain\'),
        \'parent_item_colon\' => __(\'Parent Category:\', \'text_domain\'),
        \'search_items\'      => __(\'Search Categories\', \'text_domain\'),
      ),
      \'show_admin_column\' => true,
      \'hierarchical\'      => true,
      \'rewrite\'           => array(
        \'slug\' => \'portfolio/category\',
      ),
    )
  );
}

add_action(\'init\', \'prefix_register_taxonomy\', 0);
您还应该在注册自定义帖子时注册分类法。

在此之后,请求将是:

wp-json/wp/v2/posts/?taxonomy=prefix_portfolio_categories\'&term=your-any-category
希望这可以帮助您:)

SO网友:dev

您应该使用以下选项:-

http://domain.com/wp-json/wp/v2/posts?job-type=your-post-type 
希望它能起作用:)

相关推荐