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
- Display posts in correct month order using single date custom field
- Remove a filter added by a plugin
- Change the site tagline (or similar) based on current page
- Why is the post meta[] empty when I make a call to the wordpress rest api?
- WordPress 5 WP REST routes – No errors
- tracking number field in Woocommerce order [closed]
- Handling Body class based on Template
- Get_avatar filter?
- Is there an equivalent to WP_Error object I can return in the case of a successful REST request?
- WooCommerce Related Products by Attribute
- Custom filter in admin edit custom post type responding with invalid post type?
- Add a class to the anchor tag on HTML5 gallery
- Nesting if statements to echo only one string based on what tag was filtered?
- How to properly loop through these external URLs to get them into the sitemap using this hook
- Input type search in Menu
- How can I create a list of page titles from custom meta values?
- WP Query with custom Shortcode
- add_action shortcut?
- Passing the custom field values in the wp_get_current_user array function
- Advanced Meta Query for Large Calendar Website (12k+ posts) (175k+ wp_postmeta rows)
- Sort posts by custom fields value using dropdown menu
- Is it acceptable to treat a filter like an action?
- Custom profile field with birthday. Troubles with
- WordPress – Display array data of a child
- How can I remove a function that has been added to wordpress with add_filter?
- WP_Query Check if post has one or more attached medias
- How to control WordPress image metadata (using Imagick)?
- Edit incorrect password message WordPress
- How do I add custom bulk actions to multiple custom post types?
- Add data attribute to each li in menu
- Show prices with tax in Woocommerce Mini Cart [closed]
- wordpress allow user to edit user profile with custom fields
- Why do WP_Query results change after updating unrelated Advanced Custom Fields (ACF)?
- Ajax category filtering products default show all
- Add a specific part of current category page url to shortcode
- Woocommerce hook run after an Order been created through REST API
- Why does my callback function not work with this custom filter hook?
- what is the best practice to add new field to an api route
- Store multiple custom field as post meta per post(css, js, html, 2 link) [closed]
- How to VAR_DUMP a $variable during checkout process (Is my product meta callable?)
- How to show single category archive
- Woocommerce api: create product with images – bad request
- Need Help Fixing My Iframes [closed]
- Contact Fom 7 – how to add custom HTML inside span.wpcf7-form-control-wrap AND IMMEDIATELY AFTER input.wpcf7-form-control?
- Removing “wpautop” (auto tags) only on certain pages?
- Woocommerce: hook action/filter I could use to add variation id and price with each attribute opt on WooCommerce Rest api
- Firing schema via code in functions.php doesn’t work
- Add filter when image is uploaded?
- Remove the first 5 characters of the_title and orderby that
- Remove a div from RSS feed
- Print last modified date only on posts
- Adding number to date not working [closed]
- How to add an arrow to menu items has submenus
- WordPress Gravatar filter is removing my custom attributes
- How can I add extra word in permalink when someone click download button?
- Generating an nonce for Content Security Policy and all scripts – How to make it match/persist for each page load?
- Saving and Restoring a Canvas on A Individual User Basis
- Are nonces in WP REST API optional by default?
- filter custom post in rest api with custom function
- Seach custom post type posts only by meta fields?
- How to use thumbnail size of image if I’m only using src to get image
- Unique icons next to each WordPress menu item
- 3 Slashes appear after Apostrophe in custom fields after updating product-site
- how to retrieve a value if a checkbox is checked
- how to save selected option in variable for rest api category filter
- Trouble checking if custom woocommerce checkout field is empty or not
- Two queries for a WP_User_Query search work perfectly apart, but not together
- search suggest – filter post type
- Custom query, checking values of multiple meta keys
- Archive post by meta value + 24hours
- auto-populating custom nav with all items from custom post type
- Add / Update Custom Fields After Select Pictures in Media Window
- Change tinyMCE editor to allow marked content when pasting from Word
- conditional logic for front-end custom field edits
- Problem with login form
- Trying to retrieve post meta
- Automatic Shortcode Creation with Custom Fields [closed]
- Custom field values to taxonomy terms
- How would an if statement surrounding a custom field with two variables (holding values) look like?
- Redirect to another page using contact form 7? [closed]
- How to add specific meta tags to head of cart and checkout pages in woocommerce?
- Alter required message using comment form api
- Custom Field IF/ELSE PHP
- How to debug this search & replace strings snippet?
- Hide Featured Image Meta Box on Editor Screen
- Add item to top of menu using a filter in functions.php
- Which PHP page is the Default Posts Page
- How to combine 2 php functions into one function with a custom field