It has nothing to do with WP_Query itself. The reason for such problem is what you do with this WP_Query. There’s no the_post()
in your code, so the loop is infinite.
This is how to fix it:
function bambam(){
$args = array(
'category_name' => 'breaking-news',
'post_date' => 'DESC',
'posts_per_page' => 1
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
echo '<div class="breaking-news">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<h1>' . get_the_title() . '</h1>';
}
echo '</div>';
/* Restore original Post Data */
wp_reset_postdata();
} else {
// no posts found
}
}
bambam();
Related Posts:
- How to get URL of current page displayed?
- What is This esc_html_e() i wordpress php?
- Display only text to WordPress loop without loosing the text formatting
- How to change post count in wordpress loop?
- How can I loop into two different DIVS without repeating the DIVs
- Store loop into array
- WordPress loop specific thumbnail size
- get understrap pagination to work with custom query
- Woocommerce – How to get products which have a sale between 20% to 40%? [closed]
- Displaying Only Certain Tags in Loop
- Show Custom Taxonomy Title
- Way to display “Yesterday”, “Today”
- How can i display post loop in table format?
- spliting posts into two columns
- multiple if statements [closed]
- How to select WooCommerce products by post_meta and order them
- How to Display a Single Post Excerpt
- have_posts() execution failure
- How does the ternary operator work in the wordpress loop post?
- Cannot display or echo alt text on featured image
- Using Advanced Custom Fields to create a per page slider
- WordPress Search Results for Multiple Post Types
- Changing layout with wp_customise
- Using loop pagination on single.php
- get author_name from queried post
- How to set variable, pass it to a partial file and remove it after?
- how to refresh div on template part?
- Display category name only once inside loop
- Can’t find infinite loop cause
- post thumbnail, conditions, else wont work
- Changing the_posts_navigation() html output
- WP_Query fails despite having 1 post
- wp_query get the 2nd post
- Loop stopped working
- How to loop through all the attached images in a post, and get their url one by one
- I’m trying to create an array with a foreach loop, but the array only stores the last item [closed]
- Store post content in a php variable and output them using for loop
- How to add div blocks after certain set of post
- How do I remove a category from a wordpress loop>
- Show recent posts starting at a specific number archive
- How to get the last category name of a child category?
- WordPress query undefined offset in loop
- Issue with custom loop in Archive page
- WordPress loop by meta key that is an array? and how loop multiple arrays
- Problem with custom loop and wp_list_pluck [closed]
- How to wrap every 3 posts in a div (and close the last div too) [closed]
- Dividing the loop to style post differently
- Echo array value
- First post article different on Archives template
- How to add title attribute to archive items
- Strip from or something better?
- How to break up php code to avoid echo
- Problem in replacing the_content with the_excerpt()
- Search AJAX Filters – Multiple Query Loops Into One Loop (Optimization)
- Products listing check if meta checkbox is checked
- in_array not working on dev server but works on localhost
- How to Create Carousel Indicators in PHP Loop using wp_get_attachment_url function?
- Get title of page containing post grid within the posts
- Is there anyway I can call the year once?
- Display all categories including sub categories under a list ul
- Foreach Loop Of Post Types With Nested If Else If Not Completing Process
- Where does php code to load data go?
- Adding new divs with custom classes to products on WooCommerce shop page [closed]
- cURL needing to loop through all “next_page”
- Display page content in different sections – based on page break block?
- Filtering custom posts by using checkboxes for taxonomy in an ajax loop always gives server 500 error
- Latest posts feed with a specific post always first
- Show the subcategory name based on current product
- How can I get my pagination loop to display the correct number of total pages?
- How do if all posts has this category ID then do this
- ERR_TOO_MANY_REDIRECTS / To Force SSL Logins and SSL Admin Access
- View tag description on page
- Get current_post in reverse order with pagination
- How fix error in the WordPress loop?
- Efficient way of querying for a “fallback” post?
- Insert div after every three posts in home.php [duplicate]
- Display product attributes for current product
- Div Missing In Custom Loop Query
- Loop 1 user randomly
- Create a hierarchical loop at predefined markup requirements
- enumerating custom taxonomies?
- Using page template to fetch posts in page
- Create Customization Controls from Array
- ACF loop and php formatting
- How to display thumbnail if post is assigned one otherwise not
- How to have post count after each listed category
- How can I put a custom field as the link of a button shortcode?
- Transient Loop Not working as expected
- Remove the_content From Loop
- Custom Meta Box If Else Statement
- Adding if statement to content for homepage
- Tables not showing divs and loop/php items
- Conditional in foreach loop is outputting content twice
- Alternative loop syntax error [closed]
- is there away to hide php code in wordpress not to show in PAGES
- How to create a loop that will display one post and stop?
- Woocommerce linking variations
- I want to show image from custom field image on my custom page template
- Count the number of matching post names in foreach loop
- How to get posts by a certain author in inner loop using outer loop variable or post title WordPress