这并不难。您只需添加一个条件并检查book_id
参数已传递。您甚至可以检查此参数是否正确(例如,如果book\\u id应该是一个数字,那么您可以检查它是否正确):
add_filter( \'rest_gallery_query\', function( $args ) {
if ( trim($_GET[\'book_id\']) ) { // check if book_id is passed and is not empty, but you can modify this condition
$args[\'meta_query\'] = array(
array(
\'key\' => \'book_id\',
\'value\' => trim( $_GET[\'book_id\'] ), // <- you don\'t need to esc_sql this
)
);
}
return $args;
} );