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

WP Rest API - How to get an empty response if query has no posts

programmeradmin2浏览0评论

I am using rest_{post_type}_query for some custom query params. It works fine, however, I would like to get an empty response or a "no posts found" message when the query doesn't have the posts ids to match. Currently it gets all the posts.

function query_video_by_politician($args, $request) {
    if(isset($request["politician_id"]) && intval($request["politician_id"])) {

        $mp_entry_ids = FrmEntryMeta::getEntryIds( array(
            'fi.form_id' => 41,
            'meta_value' => intval($request["politician_id"]),
            'field_id' => 517
        ) );

        $mla_entry_ids = FrmEntryMeta::getEntryIds( array(
            'fi.form_id' => 41,
            'meta_value' => intval($request["politician_id"]),
            'field_id' => 518
        ) );

        if ( !empty($mp_entry_ids) ) {
            $entry_ids = $mp_entry_ids;
        }

        if ( !empty($mla_entry_ids) ) {
            $entry_ids = $mla_entry_ids;
        }

        if ( !empty($entry_ids) ) {

            $where = array( 
                'form_id' => 41, 
                'id' => $entry_ids
            );

            $post_ids = FrmDb::get_results( 'frm_items', $where, 'id, post_id' );

            if ( $post_ids ) {

                foreach( $post_ids as $post ) {
                    if( !in_array( $post->post_id, $post_ids ) )
                        $ids[] = $post->post_id;
                }

                $args['post__in'] = $ids;

            }
        }
    }       

    return $args;
}
add_filter('rest_gallery_query', 'query_video_by_politician', 10, 2);

I am using rest_{post_type}_query for some custom query params. It works fine, however, I would like to get an empty response or a "no posts found" message when the query doesn't have the posts ids to match. Currently it gets all the posts.

function query_video_by_politician($args, $request) {
    if(isset($request["politician_id"]) && intval($request["politician_id"])) {

        $mp_entry_ids = FrmEntryMeta::getEntryIds( array(
            'fi.form_id' => 41,
            'meta_value' => intval($request["politician_id"]),
            'field_id' => 517
        ) );

        $mla_entry_ids = FrmEntryMeta::getEntryIds( array(
            'fi.form_id' => 41,
            'meta_value' => intval($request["politician_id"]),
            'field_id' => 518
        ) );

        if ( !empty($mp_entry_ids) ) {
            $entry_ids = $mp_entry_ids;
        }

        if ( !empty($mla_entry_ids) ) {
            $entry_ids = $mla_entry_ids;
        }

        if ( !empty($entry_ids) ) {

            $where = array( 
                'form_id' => 41, 
                'id' => $entry_ids
            );

            $post_ids = FrmDb::get_results( 'frm_items', $where, 'id, post_id' );

            if ( $post_ids ) {

                foreach( $post_ids as $post ) {
                    if( !in_array( $post->post_id, $post_ids ) )
                        $ids[] = $post->post_id;
                }

                $args['post__in'] = $ids;

            }
        }
    }       

    return $args;
}
add_filter('rest_gallery_query', 'query_video_by_politician', 10, 2);

Share Improve this question asked May 6, 2019 at 23:32 shubhrashubhra 1489 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I found my answer in WP_REST_Posts_Controller class

/*
* If we intersected, but there are no post ids in common,
* WP_Query won't return "no posts" for post__in = array()
* so we have to fake it a bit.
*/
if ( ! $args['post__in'] ) {
    $args['post__in'] = array( 0 );
}
发布评论

评论列表(0)

  1. 暂无评论