You can instantiate your array before-hand, optionally populate it, and pass it to the add_query_arg()
function like so:
$url_query_args = array();
if( isset( $search ) ) {
$url_query_args['search'] = $search;
}
if( isset( $category ) ) {
$url_query_args['category'] = $category;
}
if( isset( $filter ) ) {
$url_query_args['filter'] = $filter;
}
esc_url( add_query_arg( $url_query_args, '/site' ) );
Or you could loop through an array of possible query args:
$url_query_args = array();
$possible_args = array(
'search',
'category',
'filter',
);
foreach( $possible_args as $arg ) {
if( ! isset( ${$arg} ) ) {
continue;
}
$url_query_args[ $arg ] = ${$arg};
}
esc_url( add_query_arg( $url_query_args, '/site' ) );
Related Posts:
- Get all image from single page using this query
- simple sql query on wp_postmeta very slow
- How to Use Wildcards in $wpdb Queries Using $wpdb->get_results & $wpdb->prepare?
- wordpress query – orderby child post date
- Add multiple value to a query variable in WordPress
- Compare two numeric custom fields
- wp_dropdown_categories with multiple select
- How to tune search argument in WP_Query to show only exactly the same results?
- Custom query filter not working on woocommerce category page
- How to delete a transient on post/page publish?
- How to get my loop to pull posts into three columns
- Custom $wpdb Query for Custom Post Type by Category
- Can’t pass table to $wpdb->prepare
- How to extract all ID variables from a query string?
- WordPress creating excessive joins on meta_query with search
- How to display liked posts of current user in wordpress?
- Date query year and month OR just year
- Are database queries created using WordPress filters protected from SQL injection?
- get post id using the $query_vars variable
- Remove Unnecessary Mysql Query
- Grouping related postmeta data via SQL query
- How to remove duplicate query on page load or make them cacheable
- Is there a conditional tag for latest post or do i need a query?
- get_posts() loop returns the same the_title() for each post
- Huge amount of queries on my site
- How to check current user in meta value array in WP_Query meta_query
- Using pre_get_posts to target a query in the sidebar
- How select a specific query when setting offset?
- Change media attachment author via mysql query
- How do I use get_query_var() within plguin code
- How to display related posts by same publish date?
- Get post according to current taxonomy
- Ordering by a metadata subfield in WordPress?
- Alter query with posts_clauses to retrieve NULL values last
- Query based on title, with ‘compare’ => ‘IN’
- wpdb print all post meta
- Issue with using add_rewrite_rule() for post querying custom fields
- Plugin will sort users by usermeta
- How to write: $wpdb->update having WHERE NOT value pair in the array
- Exclude all sticky posts front page twenty twelve
- Query posts according to specific post meta values
- wpdb COALESCE won’t work
- empty query breaks other queries
- Problem in exploding the_content to array
- Custom SQL Query passing array to WHERE p.id
- Can’t get full post title if there a spaces in title
- Query does not return content
- What is wrong with my wp query, cant exclude the category that i want
- how to insert missing tags into the posts through mySQL?
- Query posts dynamic date range
- Parse query filter in wordpress and relation
- How to write update query in WordPress to expire transients
- Query Nickname rather than Display Name in custom Woocommerce plugin
- Get previous posts list
- how to replace old DW site with new WP site? [closed]
- Mysql query not working on WordPress 4.2.2
- WordPress SQL query to tag all posts containing a specific word on title
- get_post_meta with WP_query
- Order terms by count – missing terms
- Querying Advanced Custom Fields
- How to rearrange a pool of posts joined using two queries?
- wpdb update multiple row?
- List WordPress Post and Related Attachments outside of a post page
- How properly create a blog template for wordpress?
- Can I include get_posts in this array?
- Is there a way to add a link directly into this media query?
- Custom query Custom-Post-Types creates error
- Formatting a date/time returned from a custom $wpdb query
- Problem on displaying the results on my query
- Rewrite rule for query params
- Improve performance of slow query
- WordPress search in modified post title
- meta_query not working
- Hook into the Admin Dashboard and redirect users
- WordPress query: merge meta key (number) values and sort
- Displaying data from custom table
- SEO friendly query vars
- Question about of query
- Prevent versioning for .woff (font) files
- Orderby event date (if it exists) or post date (if it doesn’t) – how to reorder posts based on meta value OR date
- Is there any way to get all custom posts and all custom terms with it’s meta in few queries?
- Modify behaviour of “s” parameter in wp_query
- OR condition not working
- Query not returning child posts for any parent post ID
- How to make MySQL search queries with quotes
- Use $wpdb->get_results with filter based on array
- Add Hook for clearing transient when post is added
- Archive query not working correctly
- Custom MySQL Query for Post and Post Meta
- Displaying popular posts
- Find all posts via SQL beginning with A of type B
- Show links to child pages on both parent AND child pages
- learn to run wpdb class
- Modify Global Posts Plugin
- How to reference same column name but different table in custom db query
- Retrieving custom field array value through db query
- SQL Query : how copy all tags of post into their post content in wordpress by sql query
- How to add multiple values with add_query_arg?
- 3 queries to update WordPress
- How to show sticky posts on all pages of the pagination, not just the first page?