If you want to reduce code duplication, then extract the code into its own function, e.g.:
function get_xyz_query_args( ) : array {
return array('post_type' => 'accomodation', 'posts_per_page' => 4, 'meta_query' => array('key' => 'accomodation_type', 'value' => get_query_var('accomodation_type')));
}
….
$args = get_xyz_query_args();
$q = new WP_Query( $args );
However, there is very little performance to be gained by recycling the WP_Query
object, and potential losses if it’s done correctly. The complexity introduced would mean new bugs.
Since WP stores posts it fetches and post meta in WP_Cache
, the second WP_Query
only needs to fetch post IDs. On top of that, any caching plugin or object cache present can eliminate the database query completely.
So this is not worth doing if performance is your goal, there are better ways.
Related Posts:
- WP_Query with “post_title LIKE ‘something%'”?
- How to extend WP_Query to include custom table in query?
- Can wp_query return posts meta in a single request?
- compare meta_query in get_posts arguments
- How to use the_posts_navigation for wp_query and get_posts?
- How to paginate attachments in a secondary query as gallery?
- How to do a query only on a specific admin page?
- WP Cron and wp_insert_post
- How to make an activities stream mixing posts and comments?
- How to adjust found_posts so that it accounts for offset and pagination
- SQL query equivalent to WP User Query
- Function to check if author has posted within the last x days
- Single page theme that uses pages for the content
- Show default content if custom WP_Query has no posts
- WP Query for Posts (Products) in Specific Category that has 2 Specific Tags (*AND* both tags not *OR*)
- using wp_query to return posts w/ comment count > 0
- Pagination for custom query on single.php
- wp_transients | wp_object_cache VS SESSIONS & Cookies?
- How to hide post content/meta from everyone except the post author and admin
- Remove ellipsis from the excerpt retrieved using get_the_excerpt()
- Too many posts cause slow load when paginating
- WordPress query vars are not added
- When querying a combination of posts and other meta fields, is there a better solution than directly modifying the WHERE value?
- Order Posts by meta value AND published date
- Formulate a url to show posts with both taxonomy terms
- Alternative to query->set that appends conditions instead of overriding
- Group WP_Query by meta_key date
- Set if condition with wp_nav_menu
- How to reduce the number of queries?
- why update and delete query not worked in custom table?
- get_users is missing or skipping over users in loop
- WordPress Related Post by tags in Single.php
- Return Taxonomy/Term Information with Posts (WP_Query/get_posts)
- Query custom post type by custom taxonomy (category slug)
- WP_Query sorted by custom taxonomy
- problem by deleting new plugin installed inside the WordPress after any heroku app sleeping [closed]
- Use posts_groupby filter with part of string in wp_query?
- Adding multiple meta_key fields as orderby options to a WP Query via a function
- 1/3 of posts different class
- get posts by meta_key from inside key of post meta
- Get posts by list of post IDs ordered by those IDs?
- WordPress Blog Posts with Pagination inside a Page/Post
- exclude a post from wp_query loop
- Reset WordPress Post Query to default
- Is_single() conditional tag returns null in query
- Best choice for multiple loop in page?
- wp_query get data from advanced custom field in wordpress
- Wildcard search in WP Query
- Get Child Category only
- Order is breaking wp_query
- How to display all posts not in a post_format
- Especific order by Custom Field Values in WP_Query
- How to filter query loop block with a search string from the query parameters
- WP_Query – How to get all posts of specific days of week by custom field date?
- WP_Query returns no posts for category
- Querying a large number of posts including their custom fields data
- Group posts by attachment meta key
- date_query is showing duplicate results
- Post__not_in only removing first 2 pages
- How to query posts for custom post type with featured image for first 2 post?
- Query posts by searching for a string in a meta field
- Added Date Filter To Popular Posts Query
- WP_Query with 2 genres
- Display posts from an array of ID’s
- Date not appearing in custom query
- Custom post type – filter content by custom meta box content
- Trying to output content from page, works locally but not online
- How can I build a query that returns all attachments of a page and it’s children pages?
- Query ACF relationship field – Comparator IN – Value array
- Use get_cat_ID to retreive multiple category IDs
- How can I modify the query by adding to the existing query?
- WP_Query Regexp whitespace and end of string
- Custom order of posts in wp_query and different taxonomies
- WP_Query paginate with one term per page?
- Update wp_query every week
- wp_insert_post with ACF Fields and WP Meta Query returns no rows until update
- using custom meta user data to run queries in WordPress
- Look for string in posts and postmeta
- Multiple meta_query not working
- Is meta_key used for ordering, filtering, or both?
- Save query in function for custom gallery
- Merge two queries and remove duplicate
- Convert SQL Query to WP_Query
- Taxonomy Terms That Don’t Exist Display Results
- WordPress wp_query add custom query as field
- Get a page ancestor from a most viewed list
- What argument does my function need to echo get_results() query results
- Custom pagination (Title, date and teaser)
- Using custom field as ‘orderby’ value causing post grouping by publish date
- Sorting Posts with meta value not working
- Display posts from catagories
- Add pagination to table generated by wp_query
- Fire query on ajax post url page
- using next-page and previous page inside a wp_query
- What is wrong with this pagination code?
- how to add limit records in wordpress query
- query only direct child and sub-terms of a current term archive
- restrict posts per page
- WP_Query by meta key not returning any posts
- Display Featured image from custom post type category (custom taxonomy) wise