Since wp_reset_query()
and wp_reset_post()
didn’t appear to do anything in this situation, here’s something that seems to work. I’m not entirely sure if this is the accepted approach, but I got it working by temporarily storing the $post value before the WP_Query, then setting it back to the $post variable after the query was complete.
$currentpost = $post;
$query = new WP_Query(/* ... */);
while($query->have_posts()){
$query->the_post();
//do stuff
}
$post = $currentpost;
Related Posts:
- How to Display Custom Post Type’s Gallery (images ) in Through WP_Query
- Finding posts containing matching array elements in a meta field usign WP_Query
- Slow WP_query due to nested wp_query. Need Suggestions
- Get Posts by IDs (optionally)
- WP_Query leaking absurd amounts of memory
- Register custom query args parameter for WP_Query()
- get_template_part from plugin
- How to add multiple custom URL variables?
- How to control output of custom post type without modifying theme?
- Getting wrong relationship value in $args in wp_Query?
- Check for featured image in WP_Query
- Why Does get_posts() Return an Empty Set?
- Unit testing to simulate the loop
- A Post is saved twice or more during add_action(save_post)
- How to get data from WordPress $wpdb into React Gutenberg Blocks Frontend?
- get_posts / WP_Query Memory size of 134217728 bytes exhausted
- How to ensure “the_content” filter runs only for the main displayed content?
- add_filter and remove_filter added before and after wp_query
- How to get all images and their thumbnails from wp media library
- How to create a dynamic page based on form data with a plugin?
- Conflicting save_post functions when passing the post id and saving custom meta boxes for different post types
- Handling form request from plugin file
- Paging on a future post loop?
- save_post hook not triggered in WP v3.6.1
- Cannot search post by taxonomy
- How to get orders with used coupon in WooCommerce
- Support auto-save and revisions for custom fields
- WP_Query with tax_query, order by most ‘matches’
- How to print raw query from WP_Query class just like in CodeIgniter
- WP_Query returns no results
- Create a custom display order in the main menu
- manage_posts_custom_column, showing serial number using increment in loop
- Error : “Cannot use object of type WP_Post as array in”
- Proper way to run wp_query from inside a plugin
- get_the_excerpt() not working in plugin loop
- ORDER BY wp_post custom column name in wp_query
- How to create database table, add data, update and delete using wpdb via plugins?
- Plugin with action ‘save_post’ needs to press publish twice on order to publish
- Strange issue saving custom field data for a WooCommerce order
- Elementor custom Query with ACF fields to show matching woocommerce products custom fields
- WP_Query filters
- How to get Metabox custom field to show checked if value is updated using post meta query?
- WordPress pre_get_posts with combined results of two queries (OR)
- Search: how to extend the existing search to include a custom table
- how to get all the child category name in a specified category name?
- how to invoke wordpress API from other existing PHP system
- How I can use order by of the custom post title?
- Checked() function on a multidimensional array
- How to get the post excerpt using post object?
- Delist entries in the_loop
- How to query children by post name/slug
- 400 Bad Request, in wordpress theme development, wp_ajax
- Prevent WordPress automatically processing certain URL queries
- Store custom meta box data as serialized array
- WordPress publish_post hook not getting featured image and meta on first publish, but works on updating title
- Add function after the_content
- how to load the comment template from a plugin
- How remove trashed WooCommerce orders from wc_get_orders() result?
- Auto-complete or auto-suggest from stored data in database
- Get audio metadata on file upload
- Get post thumbnail in WP_Query
- Pagination not working with custom wp_query
- How to get changed post title in my custom plug-in which fires when ‘save_post’ is called?
- Adjust query on single
- rewrite_rules problem
- WP_Query order posts by category
- Is it possible to use the MySQL’s ORDER BY FIELD() clause as part of the pre_get_posts hook?
- Count products with custom metadata field in an order
- Multiple dynamic Tax Query – pass taxonomy argument from array
- Is there a way to get rendered html content of a WP post after updating?
- WP_Query not ordering correctly
- How to check category name available or not
- Home page is not loading, where in other pages are displaying on the site
- how to create a shortcode from a variable in plugin
- User meta query using Wildcard
- Don’t print customizer styles when no setting has been used
- Group By in a Metaquery
- WP Query date_query with several date range
- How to include files in the loop via ajax
- Saving post meta
- Making Woocommerce optimized for more than 500k products
- Post Pagination does not working on WP-Query
- WP Query Relations / Compare
- pull data from wordpress database
- How can I query 3 posts from diferente categories (one of each category)
- Query only title/field/featured media of posts [closed]
- Error in WP Query. If variable is empty it is displaying previous post value
- post_results filter confused by pagination in sidebar and main section
- save_post not called in plugin / custom field still saving overwriting metaboxes
- Can’t get query string in ajax call
- Get custom wp_query search results to appear on search.php
- Best Way to Grab Post ID from Plugin
- How do I query posts and have their related taxonomies returned in the results?
- Loop output for custom plugin [solution found]
- $_GET vs get_query_var()
- Sanitize and Save metabox values
- What is the meta_query key name for the woo product average rating? [closed]
- get the queried_object of an url
- Return custom product in ajax call loop
- filter on get_posts efficiently