自定义REST端点无法检索单个帖子(“REST_NO_ROUTE”)

时间:2020-05-03 作者:Paolo Sacchetti

我的自定义REST端点有以下基本路径:

add_action(\'rest_api_init\', \'my_cpt_route\');
function my_cpt_route() {
    register_rest_route(\'mytheme/v1/\', \'my_cpt\', array(
        \'methods\' => WP_REST_SERVER::READABLE,
        \'callback\' => \'my_cpt_results\',
    ));
}
这是CB功能:

function my_cpt_results() {
    $args = array(
        \'post_type\' => array(\'my_cpt\'),
        \'posts_per_page\' => -1,
    );
    $mainQuery = new WP_Query($args);

    $results = array();

    while ($mainQuery->have_posts()) {
        $mainQuery->the_post();
        
        array_push($results, array(
            \'id\' => get_the_ID(),
            \'title\' => get_the_title(),
            \'url\' => get_the_permalink(),
            \'image\' => array(
                \'small\' => get_the_post_thumbnail_url(0, \'thumbnail\'),
                \'medium\' => get_the_post_thumbnail_url(0, \'medium\'),
                \'large\' => get_the_post_thumbnail_url(0, \'large\'),
            )
        ));
        $results = array_values(array_unique($results, SORT_REGULAR));
    }
   
    wp_reset_postdata();
    
    return $results;
}
当我检索所有帖子时,这一切都很好(因此如果我转到;http://mysite.test/wp-json/mytheme/v1/my_cpt/";我得到了所有的CPT帖子)。

我所期望的是,如果我在URL的末尾附加一个已发布的CPT帖子的id(例如:“;http://mysite.test/wp-json/mytheme/v1/my_cpt/56";)我只需要拿到id为56的帖子。相反,我得到了一个404“;rest\\u no\\u路由;。也许我需要修改register\\u rest\\u route args?(但是怎么做?)。。。

更新

。。。如何使用这些函数作为类方法获得相同的结果?我不太喜欢OOP,我想学习。。。假设我有以下简单的类(基于wppb.me插件样板):

class My_Routes {
/**
 * The ID of this plugin.
 *
 * @since    1.0.0
 * @access   private
 * @var      string    $plugin_name    The ID of this plugin.
 */
private $plugin_name;

/**
 * The version of this plugin.
 *
 * @since    1.0.0
 * @access   private
 * @var      string    $version    The current version of this plugin.
 */
private $version;

/**
 * Initialize the class and set its properties.
 *
 * @since    1.0.0
 * @param      string    $plugin_name       The name of this plugin.
 * @param      string    $version    The version of this plugin.
 */
public function __construct($plugin_name, $version)
{

    $this->plugin_name = $plugin_name;
    $this->version = $version;
}

/**
 * Creates routes
 */
public static function register_cpt_routes()
{
    register_rest_route(\'mytheme/v1/\', \'my_cpt\', array(
            \'methods\' => WP_REST_SERVER::READABLE,
            \'callback\' => array( $this, \'get_posts\' ),
        ));
    register_rest_route(\'mytheme/v1/\', \'my_cpt/(?P<id>[\\d]+)\', array(
            \'methods\' => WP_REST_SERVER::READABLE,
            \'callback\' => array( $this, \'get_post\' ),
        ));
}

/**
 * Callback function to load all posts
 */
public static function get_posts()
{
    // ...do stuff for getting all the posts, and this is working
}

/**
 * Callback function to load a single post
 */
public static function get_post($data)
{
    // How do I pass the $data args to the get_post method?
    
    $cpt_id = $data[\'id\'];
    // ...do stuff
}
}

1 个回复
最合适的回答,由SO网友:moped 整理而成

要获得预期结果,必须创建第二个端点。您的端点不知道如何处理路径的最后一部分(ID)。

register_rest_route(\'mytheme/v1/\', \'my_cpt/(?P<id>[\\d]+)\', array(
    \'methods\' => WP_REST_SERVER::READABLE,
    \'callback\' => \'my_cpt_single_result\',
));
您的CB函数可以如下所示:

function my_cpt_single_result( $data ) {
   $cpt_id = $data[\'id\'];
   // make your query with the $cpt_id
}
应该再做一点验证。它被解释了here 在更长的版本中。