最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

posts - Custom wp-query in wordpress rest api

programmeradmin5浏览0评论

For example I have a query with tax and metas and other stuff..

how I can pass it to the rest api so i can call all the data with one call

for example this function

function load_special_args() {
    $args = array(
        'post_type' => 'post',
        'order' => 'DESC',
        'posts_per_page' => 100,
        'tax_query' => array(
            array(
                'taxonomy' => 'category',
                'field'    => 'term_id',
                'terms'    => '9995',
            ),
        ),
    );
    return $args;
}

imaging call: /wp/v2/posts?args=load_special_args&term=9995

Because I want a special use i dont want to use the /wp/v2/posts?categories=id

For example I have a query with tax and metas and other stuff..

how I can pass it to the rest api so i can call all the data with one call

for example this function

function load_special_args() {
    $args = array(
        'post_type' => 'post',
        'order' => 'DESC',
        'posts_per_page' => 100,
        'tax_query' => array(
            array(
                'taxonomy' => 'category',
                'field'    => 'term_id',
                'terms'    => '9995',
            ),
        ),
    );
    return $args;
}

imaging call: /wp/v2/posts?args=load_special_args&term=9995

Because I want a special use i dont want to use the /wp/v2/posts?categories=id

Share Improve this question edited Sep 13, 2018 at 22:38 user2670708 asked Sep 13, 2018 at 22:07 user2670708user2670708 151 silver badge5 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You can create a route for Rest API using the below code. Simply put this code in your function.php file

/* Route For api */
add_action( 'rest_api_init', function () {
    register_rest_route('wp/v2', 'test', array(
        'methods' => array('GET', 'POST'),
        'callback' => 'test',
    ));
} );

And then simply create a callback function test and put your stuff in to that function

function test(){
  // put your stuff here
}
发布评论

评论列表(0)

  1. 暂无评论