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?
- Most efficient way to add javascript file to specific post and/or pages?
- Display all values of custom a field created with ACF on a page
- If Custom Field is empty don’t display div
- How to update single value in multi dimensional Post Meta?
- Add custom field to attachments in a specific category
- Create a new user using WP REST API and declare meta object
- How can update custom meta for all posts
- “Maximum function nesting level of ‘100’ reached” after adding a new filter
- Find variables available at a given hook
- wp_get_attachment_url filter won’t accept two arguments
- get_post_custom single array
- Make custom field meta not display if there is not data in it
- How do I fetch all comments per post via WP REST API?
- When using the_author hook, how can I determine the PHP file that generates each call to `the_author()`?
- Combine two filters into a single call
- WordPress 5 WP REST routes – No errors
- WP Query with custom Shortcode
- Custom profile field with birthday. Troubles with
- How do I add custom bulk actions to multiple custom post types?
- wordpress allow user to edit user profile with custom fields
- what is the best practice to add new field to an api route
- How to VAR_DUMP a $variable during checkout process (Is my product meta callable?)
- Add filter when image is uploaded?
- Remove a div from RSS feed
- how to save selected option in variable for rest api category filter
- Alter required message using comment form api
- How to combine 2 php functions into one function with a custom field
- Add filter multiple times using only one master function
- Saving zero in meta box
- How to edit php code in WordPress Post file?
- Change page title from page using php via php executed from page/post itself
- Omit custom post type from wp-sitemap.xml based on meta key using wp_sitemaps_posts_query_args
- Why does this update_post_meta function not delete the custom field itself?
- Permission callback to check if user has application password
- Create page template via functions.php?
- WC_Customer delete function returns error
- How to have different site identity logos on each page on Astra Theme [closed]
- WordPress loop by meta key that is an array? and how loop multiple arrays
- WP All Import / Update stock quantity from multiple XML files
- How to hide posts of a specific custom category in WordPress?
- REST API works in browser and via AJAX but fails via cURL
- Concatenate Custom Field Value & HTML Value
- `update_post_meta` not working anymore
- Hide a div when a custom field is empty
- Filter unique custom field value based on custom taxonomy category or other custom field
- Add “alt” Attribute to GD Star Rating Tags
- Display background color or image with custom meta box?
- Can ‘Custom Field’ data be inserted into this Short Code?
- Query pulling a single post per month
- How to internationalize header image?
- Add custom fields from different posts
- apply_filters with several different values?
- How to add a custom filter (by coding) before access one wordpress page ? And where to call the custom filter?
- WP_REST_Response – How to return Gzip answer and add Content-encoding header?
- Adding a custom meta field with default NULL value which is not selectable
- WordPress api post image raw data without being blank in media library
- Products listing check if meta checkbox is checked
- meta_query search names when they have middle initials
- How to update custom fields when post is published?
- Hide a div if the fiels is empty
- WordPress 5.6 Application Passwords
- Inserting a class on before_widget if checkbox is checked
- Shortcodes not processing inside post content
- Call WP Rest-Api to GET /users/me returned NOTHING in console
- I want to hide “sold by” on certain pages with id page 43
- What is the correct way to search 3 custom fields only in WordPress?
- wp_get_nav_menu_items() with ACF
- WP meta_query args not working in function
- How do I get the value of a current user’s custom field?
- Need to forward Data from WooCommerce Webhook sent to same site WordPress REST API custom endpoint
- How to filter specific element of an array in wordpress/php?
- Require advice handling a URL redirect from a Third Party. URL Params need to populate and then forward to payment
- How to generate customized filtered output for the gallery
- Add class to all meta boxes for a custom post type
- Add a leading zero to pagination
- Getting a value from a custom field from a page that shares a parent with the current page
- How to apply filter
- Custom fields changes the formatting of metabox input
- Display Content Based on Custom Field Value
- query post based on comparison
- How can I put a custom field as the link of a button shortcode?
- WordPress – show all posts + ones with specific tag
- Where can I find the code for the menu page meta boxes?
- I want Page titles and excerpts to show up on home.php in certain order
- REST API custom endpoints for metaboxes
- How to initialise WP_Query on the basis of a specific meta_value and continue iterating rest?
- Custom Admin Menu Report for Specific User ID
- Add text below WooCommerce short description if metabox value is true
- ACF number less than comparison not working