admin管理员组文章数量:1026989
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
1 Answer
Reset to default 0I 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 );
}
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
1 Answer
Reset to default 0I 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 );
}
本文标签: WP Rest APIHow to get an empty response if query has no posts
版权声明:本文标题:WP Rest API - How to get an empty response if query has no posts 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745515440a2154022.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论