You can put your meta_query
in a variable to achieve the desired result. Then you can fill the variable with your actual meta_query when your condition is met. Check out the code below.
$meta_query = array();
if ( isset($_GET['scope']) && !empty($_GET['scope']) ) {
$meta_query[] = array(
'key' => 'name',
'value' => $_GET['scope'],
'compare' => '='
);
}
$foo = new WP_Query([
'post_type' => 'people',
'posts_per_page' => -1,
'meta_query' => array(
$meta_query
)
]);
Related Posts:
- Get Current User Id Inside a Loop Returns 0 For a Shortcode
- WP_Query adds “(wp_posts.ID = ‘0’)” so no results are returned
- WordPress SQL JOIN query
- get_terms with specific id order
- get different meta-data of a complicated query at the same time
- WordPress search query, how to modify the sql
- SQL Query Search page
- direct query to post_meta table
- How to sort search result by post_title, then by post_content
- Best Practice for PHP
- How to get the list of WooCommerce product image of a certain category from database?
- Alter query on edit.php
- retrieve thumbnail from post ID of best selling product in category
- How to Join two tables from separate databases within WordPress
- How to create a WP_Query to search the Title or Tag?
- how to display active, upcoming and past event with featured listing with pagination
- Get list of WP Updates Across Sites
- if statement on database query
- WP_Query meta_query results date by date
- Enhanced WordPress Search
- WP_Query sort using multiple numeric meta values
- Help with adding pagination to custom wp_query
- register_activation_hook isn’t adding table to DB
- How to generate page content from database the right way?
- How to get to a specific position in wp_query?
- API response to be stored locally
- Use wp_get_recent_posts with search term
- WP_Query loop, does not display special characters in title correctly
- Remove query string specific key value
- phpMyAdmin displays error when importing database
- Preferred Method of debugging a wordpress SQL calls?
- How to filter posts by post format “standard” from wp-json api?
- Using foreach loop breaks
- Importing Geo data into wordpress database
- Loading the same WP_Query in two different wordpress .php templates
- How to prevent WP_Query function from returning all posts when empty?
- Get users that likes the post
- inserting a post from an extern php file but post content doesn’t show on wp site
- Create a Blog Template Page
- Exclude posts with specific metadata from search?
- Display an image if odd number of posts in grid
- Why doesn’t PhotoSmash plugin play well with wp_query?
- How to show meta value code HTML after x paragraph
- Form search query – displaying ACF “Post Object” field as results
- get_the_ID() doesnt work
- change the default order of posts only for specific categories
- Pull MySQL data from multiple tables and merge into 1 PHP array
- extract serialized array to use for wp-query
- How can I split my query result in 2 arrays?
- How to get post category list as select in front-end?
- Order a WP_Query by meta value where the value is an array
- How do I check if there is a tag in the title
- Using multiple variables to assign categories to an array
- Search.php gets metadata from first post
- the_post(); prints out style text into my HTML?
- quotes problem in very simple sql
- orderby meta_value_num is not working, giving default order
- How can I modify this code to make the search box include tags and meta
- WP_Query for woocommerce products with a pattern as a post_title
- WordPress Search Ajax + Isotope + InfiniteScroll
- How do I make these combination select filters work when only one dropdown is set? They work in WordPress 5.8.2 but don’t in 5.8.3
- How to call my PHP function with AJAX ? wp_query
- Fetch Data from an external MSSQL Database in a Managed WP hosting
- Ajax filter with loadmore button
- Query by pagename not Working
- Display featured image of post type category
- Delete post meta by serialized meta value
- can I extend the WP_Query class to deal with ‘duplicate’ posts created by joining to wp_posts?
- Getting info about selected posts using one WP_Query
- WP_Query To Display Product Of Brand On Taxonomy Page
- How to display last whole post on the homepage
- Query doesn’t display text data with apostrophes
- How to return count of items found in SQL query
- Where to check in PHPmyAdmin / SQL database for subdomain
- wp_query beginner
- Plugin Modification Change Functionality For Logged User Only
- For each 3 posts, show a different post type
- WooCommerce – Print Processing orders [closed]
- Conditional statement within WP SQL query
- Weird Behaviour: Not all WordPress Posts appearing
- Can not manually create a database ( db, user, pass ) for a plugin
- List of child pages fetch next results at link click
- How to merge 2 WordPress sites?
- Query category-specific, paginated posts and allow viewer to change sort order
- wordpress is_main_query() breaks
- allow previews outside wordpress folder (Outsourcing WP previews)
- How to get category pages to look like a certain archive page?
- Post Title Not showing up
- Advanced Taxonomy query, with 3 post types
- reorder a WP_Query, using a dropdown
- Custom query_posts() parameter
- Why does a meta_query break this WP_Query?
- How to modify this function to exclude also the post belonging to a specific category?
- Display biography post for today’s birthday person
- Changing regular db connection to $wpdb
- Can’t send form data to wpdb when URL has query string
- XML WP_Query problem in PHP 8.0 and 8.1
- How to initialise WP_Query on the basis of a specific meta_value and continue iterating rest?
- How do I display WooCommerce products in my query to rows of 3?
- How to get posts by a certain author in inner loop using outer loop variable or post title WordPress