按自定义分类发布的自定义终结点过滤帖子

时间:2022-02-12 作者:Jorge Amaya

我正在尝试通过自定义分类法创建一个自定义端点,我已经为这些类别创建了另一个端点,它工作得很好,但是有一些问题我无法解决

function get_latest_posts_by_term($request) {

    $args = array(
            \'term\' => $request[\'term_id\']
    );

    $posts = get_posts($args);
    if (empty($posts)) {
    return new WP_Error( \'empty_term\', \'There are no posts to display\', array(\'status\' => 404) );

    }

    $response = new WP_REST_Response($posts);
    $response->set_status(200);

          $data = [];
          $i = 0;

    foreach($posts as $post) {
        $data[$i][\'id\'] = $post->ID;
        $data[$i][\'title\'] = $post->post_title;
        $data[$i][\'excerpt\'] = $post->post_excerpt;
        $data[$i][\'date\'] = $post->post_date;
        $data[$i][\'content\'] = $post->post_content;
        $data[$i][\'slug\'] = $post->post_name;
        $data[$i][\'author\'] = get_the_author_meta(\'display_name\', $post->post_author);
        $data[$i][\'category\'] = get_the_category( $post->ID);  
        $data[$i][\'theme\'] = get_the_terms($post->ID,\'theme\');
        $data[$i][\'Rute-geografic\'] = get_the_terms($post->ID,\'rute-geografice\');
        $data[$i][\'featured_image\'][\'thumbnail\'] = get_the_post_thumbnail_url($post->ID, \'thumbnail\');
        
        $data[$i][\'post_tags\'] = get_the_terms($post->ID,\'post_tag\');
        
        
        
        $i++;
    }

    return $data;
}

add_action(\'rest_api_init\', function() {
register_rest_route( \'wl/v1\', \'posts/term/(?P<term_id>\\d+)\',array(
                \'methods\'  => \'GET\',
                \'callback\' => \'get_latest_posts_by_term\'
      ));
    
});

1 个回复
SO网友:Jorge Amaya

已解决!!!我的一个朋友找到了路

function get_latest_posts_by_term($request) {

    $parsed_args=[
    \'numberposts\'      => 20,
    \'orderby\'          => \'date\',
    \'order\'            => \'DESC\',
    \'post_type\'        => \'post\',
    \'tax_query\' => array(
        array (
            \'taxonomy\' => $request[\'slug\'],
            \'field\' => \'slug\',
            \'terms\' => $request->get_param(\'name\'),
        )
    ),
    ];
    
    $get_posts = new WP_Query;
    $posts= $get_posts->query( $parsed_args );
    //$posts = get_posts($args);
    if (empty($posts)) {
    return new WP_Error( \'empty_terms\', \'There are no posts to display\', array(\'status\' => 404) );
    }

    $response = new WP_REST_Response($posts);
    $response->set_status(200);

          $data2 = [];
          $i = 0;

    foreach($posts as $post) {
        $data2[$i][\'id\'] = $post->ID;
        $data2[$i][\'title\'] = $post->post_title;
        $data2[$i][\'excerpt\'] = $post->post_excerpt;
        $data2[$i][\'date\'] = $post->post_date;
        $data2[$i][\'content\'] = $post->post_content;
        $data2[$i][\'slug\'] = $post->post_name;
       
        $i++;
    }

    return $data2;
}

add_action(\'rest_api_init\', function() {
 register_rest_route( \'wl/v1\', \'posts/terms/(?P<slug>[a-zA-Z0-9-]+)\',array(
                \'methods\'  => \'GET\',
                \'callback\' => \'get_latest_posts_by_term\'
      ));
    
});```

相关推荐

如何在WordPress 4.7+中使用REST-API进行元查询?

我试图通过元查询进行过滤,但无论我尝试什么,我都无法使其工作。基本上,我希望通过rest API进行此查询:wp-json/wp/v2/books?meta_query[relation]=OR&meta_query[0][key]=arkivera&meta_query[0][value]=1&meta_query[0][compare]== 我尝试了几个不同的自定义字段,但它总是返回所有post对象。在我的函数文件中,我添加了:function api_allow_m