I’m not certain why it would cause an infinite loop, but make sure not to use $wp_query
as the variable name for your custom query. It’s a reserved global variable for the main query. Use a different name for the variable. I’d also suggest using wp_reset_postdata()
after the loop:
$my_query = new WP_Query( $args );
if ( $my_query->have_posts() ) :
while ( $my_query->have_posts() ) {
$my_query->the_post();
}
else:
echo "nothing found.";
endif;
wp_reset_postdata();
Related Posts:
- Return parent post with its children using WP_Query?
- Where should you reset postdata?
- Differences Between WP_Query() and get_posts() for Querying Posts?
- Escaping WP_Query tax_query when term has special character(s)
- Ajax and WP_Query/tax_query parameter
- Loop through all tags & output posts in alphabetical list
- Sorting Posts by custom field
- Order posts by date and then by custom field
- Hide posts from users with a specific role
- WP_query category__in not working, only pulls from first category
- tax_query shows no results if nothing is selected
- meta_key and meta_value not working together
- Show most popular post of last 12 months
- WP_Query to get posts in a specific category and post format
- Can’t get drafts with WP_Query using post_status parameter
- get_query_var returns null
- Get ONLY the count from wp_query without fetching posts
- Different string for specifed post type on posts listing at homepage
- How can I order Wp_Query hierarchically?
- Order by custom table and multiplication
- WP_Query ajax loader detect end of posts
- How to get count of posts assigned to given category?
- WP_Query posts by distance based on LAT & LNG in Database
- Changing WP_Query params with url Query Var
- From where is archive.php getting its posts?
- Wp redirect to url with ampersand string
- Get attachment by meta_key value
- Complex WP_Query order request: DESC by day, but then ASC by time
- How to detect if query is runned from widget?
- Change query based on post type while staying in loop
- Access WordPress query by index number outside the loop
- Custom Pagination Layout for Elementor widget
- Pagination outside of loop
- Multisite – Global CPT – advices?
- $WP_Query: How to display excerpt on first post only
- How to properly reset a nested WP_Query query
- How to make search for posts using get method?
- Gallery shortcode numerical sorting
- Is there a reason why Pages are not publicly_queryable?
- Check return value of get_posts
- How Can I Change The Tax Query For The Main Loop For Taxonomy Archives?
- Exclude some authors from query
- pre_get_posts – Trying to get property of non-object warning
- Replacing search results with custom external query
- Can’t get simple meta queries to work
- How to use if($wp_query->query_vars[‘name’] == ‘pagethatdontexist’) without getting a 404? How to suppress 404
- complex get_posts() query to select child pages
- WP Job Manager plugin – Listing only the twelve job categories on frontpage order by jobs they have
- Trouble building a custom WP_query
- Is there a way to join WP_Query without a filter?
- Multiple meta_key ordering with pre_get_posts
- Using WP_Query for categories instead of get_terms
- wp_query get attachments in larger size
- Force WordPress to be more random
- Show posts of an advanced search form
- Query using string from custom field as array value
- Category based on post id
- posts_per_page is not working
- Widget only in first post
- Query WP Page for Buddypress Group Home [closed]
- How can I change the term “Published” next to # of posts published on WordPress dashboard All Posts page?
- WP Query orderby ACF boolean values and ACF field value
- Get Post Title from different post type based on current loops post_parent using
- How can I order a post query’s results based on the number of matching taxonomy terms?
- wp query to use both author id and meta_query
- How to make the WP query search for the “s” arg in other places too
- WP_Query for a taxonomy value OR a custom post type meta field
- WP_query pagination on frontpage
- WP Query by 4 different taxonomies
- WordPress Query by Category using Post Slug
- Select multiple categories with is_tax
- WP Query for all events prior to current date
- Display Search Results by tag_ID in my search.php
- Repeat array inside array through while loop
- What’s causing an infinite loop?
- Displaying posts from sub taxonomies only
- Logging search queries for logged-in users
- pages shortcode filtering by category
- get term objects and post objects in query
- Ignoring ‘a’ when sorting posts
- Issue generating custom filter for cpt admin columns
- Duplicate a Query Built with PHP and Mysql but now in WordPress
- How to take parameters from a function and make them editable as attributes in a shortcode
- Object of class WP_Query could not be converted to int inside shortcode
- display post multiple times based on array of dates
- What method does this eventually call: do_action_ref_array( ‘parse_query’, array( &$this ) );
- WP_Query secondary query failing
- Custom search (wp query by custom fields)
- pre_get_posts or $where, which one to use?
- Sorting is not working in WordPress WP_Query
- Add custom WP_Query after the first 3 posts
- Limit the number of posts from a specific category on index.php
- Some images not being returned with wp_get_attachment_image
- Weird results using wp-query with ‘date_query’ for last week
- Why won’t my paged wp_query work? [duplicate]
- Query for page content, and query for posts on the same page?
- Limit the number of posts via wpquery
- Using ‘meta_query’ with the ‘pre_get_posts()’ hook disables searching for post titles
- Woocommerce set loop_shop_columns to be 3 in the main shop page loop only and 4 otherwise
- How to save the results of a query as a php file for an autocomplete search bar