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 );
}