It’s not so difficult. All you have to do is to add a condition and check if the book_id param is passed. You can even check if this param is correct (for example if the book_id should be a number, then you can check if it is):
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;
} );
Related Posts:
- Is it possible to retrieve all posts with a certain value for metadata?
- WP_Query: getting posts where custom field exists
- User Meta Value not echoing despite Var_Dump Showing correct string
- Filter by field with array value in ACF on WP REST API
- How to sort WooCommerce products page by latest in-stock items first?
- WP_query meta_query slow with OR and DATES
- Get meta_query value by user meta array
- How to use something like meta_query but for fields?
- Custom field in title
- Too many if’s and else if’s ?? – Must be better way [closed]
- How to pass value to add_filter wpcf7_form_tag from another function?
- Custom rest fields not loaded in rest api cpt response
- Set media upload attachment link to none and hide it in WP v3.5
- How to set custom cookies before output
- How to loop through JSON data in wordpress WP REST API
- Rename image uploads replacing “width” and “height” in filename with “medium”, “large”, “small”
- How to display multiple Post meta_key/meta_values by SQL query
- Rename image uploads with width in filename
- Check if page parent has certain template
- How to loop over custom fields in a page template?
- Why do filters/actions require an argument count?
- Removing WordPress profile fields from non-admins
- Getting my head round WordPress filter
- How to display custom field on homepage
- List of posts by day of the week
- How to set a custom path, for ajax image upload folder in admin?
- Show custom field if it exists, and show different elements if it doesn’t
- How to use return in my custom function instead of echo
- WordPress php filter admin_body_class not working
- Remove a filter added by a plugin
- Is there an equivalent to WP_Error object I can return in the case of a successful REST request?
- Add a class to the anchor tag on HTML5 gallery
- How can I create a list of page titles from custom meta values?
- WordPress – Display array data of a child
- How to control WordPress image metadata (using Imagick)?
- Why do WP_Query results change after updating unrelated Advanced Custom Fields (ACF)?
- Need Help Fixing My Iframes [closed]
- Removing “wpautop” (auto tags) only on certain pages?
- Adding number to date not working [closed]
- Saving and Restoring a Canvas on A Individual User Basis
- How to use thumbnail size of image if I’m only using src to get image
- 3 Slashes appear after Apostrophe in custom fields after updating product-site
- Custom query, checking values of multiple meta keys
- Advanced Custom Fields – display label and value only if value entered
- Filtering a function’ output for a new continued function
- Delete taxonomy and delete all post related it
- Deleting Certain terms from appearing on the front end as links
- How to set a template with wp_insert_post
- Multiple requests external data api dynamic block gutenberg
- Add meta tags to a custom header
- Ajax filter button display all posts
- if custom field doesn’t exist, use post thumbnail instead, as image background
- How to hide posts of a specific custom category in WordPress?
- How to clone all WordPress Rest API end points
- Get the name of menu item with wp_nav_menu
- update_post_meta() not saving data inside of save-post filter
- How to add aria role and schema markup to custom walker container
- Define category ID using get_post_meta
- Adding custom url to readmore link using get_permalink()
- Add “alt” Attribute to GD Star Rating Tags
- custom header text
- How to internationalize header image?
- Add custom fields from different posts
- Flatten Responses returned via WP REST API via WP_Error for obfuscation
- User filter posts by year
- Delete images from media library when user deletes an image from ACF Gallery
- Isn’t Returning Value While Using SELECT COUNT(*) FROM {$wpdb->prefix}
- Order category posts by last word of custom field
- Is it possible to order posts using multiple meta queries, i.e. show posts from first meta query, then the second?
- Block error message in foreach loop when looping through ACF field
- WordPress 5.6 Application Passwords
- How to use apply_filters() inside a plugin class?
- Multiple Tag Filtering
- WooCommerce REST API AJAX Auth – 401 response
- Looping through and combining calls to Woocommerce REST API
- How can i style “echo apply_filters”
- Meta query not showing result properly
- Add class to all meta boxes for a custom post type
- How to display custom field from a gallery thumbnail?
- Getting a value from a custom field from a page that shares a parent with the current page
- Efficient way of querying for a “fallback” post?
- Select All not working in a WordPress search filter
- If custom field doesn’t exist, or exists and it’s true show title?
- Outputting a custom field in PHP
- Custom fields randomly stop working
- Where can I find the code for the menu page meta boxes?
- WXR XML import is stripping php tags that I need to keep
- AJAX: WordPress filters inside $html do not work as intended
- Why not showing all post by default in my jquery filter
- Metadata on the WP_Post object
- Appending an ACF custom field to the page title
- Assign new post author IF another user in custom field on post transition
- Blog Posts not sorted and pagination not adjusted after filtering some category
- Displaying custom meta box value in a custom post page
- How do you create a front end form that enables the editing of member-specific custom fields in WordPress?
- How to execute a shortcode within a custom field?
- Is there a hook that I can use when a fatal error occurs?
- Problem with custom user fields default value and retrieval
- Cannot Access ACF Field Values via my Plugin
- REST API Plugin Update call back not updating the plugin