The problem: The code never cleans up after its queries
Your queries are currently missing several things:
- The code never checks that the query found any posts via
if ( $q->have_posts() )
- There are no calls to
wp_reset_postdata
When you call either the_post
or setup_postdata
, you change the global $post
variable. This can cause issues in any code that runs after the loop as the post context is no longer the post of the page, but the last post of the query loop.
Calling wp_reset_postdata
should fix this, but it must only be called if the query found posts. If this isn’t done, then this can cause issues or slowdown in nested loops, and other unanticipated behaviour.
Here is an idealised WP_Query
loop:
$args = array(
// ....
);
$q = new \WP_Query( $args );
if ( $q->have_posts() ) {
while( $q->have_posts() {
$q->the_post();
// display post
}
wp_reset_postdata();
} else {
echo 'no posts found';
}
Related Posts:
- Submit remote form to wordpress REST API and save data to custom post type
- How to print all the inline styles in the WP_Styles instance together with their keys?
- Looping through dummy posts and showing them in the main index.php
- Exclude category from woocommerce product category list
- Create dashboard widget for custom theme support
- Create category after theme setup and modify the default one
- Should I set a page as “No-index” if I include it’s content within the front-Page.php via this method?
- Trying to get pagination working on WP_Query() post grid
- alternate left – right content inside the loop
- Store user form submitted information in post type
- Display attached images of a page or post that are insetred using gallery
- Add a custom link to each image in WordPress gallery
- add custom link to gallery images
- register dependency css and js inside a plugin class
- Proper use of Output Buffer for a whole php clas
- Warning: count(): syntax problem of the count () function
- WordPress files break if I edit them, but adding a closing PHP tag fixes it
- Collapsible menu on post sidebar only expands and does not collapse
- Fix hover images blink
- WordPress child theme, creating a custom php template page
- Menu to the right of screen on desktop using Bootstrap 4
- Custom theme admin-ajax.php 400 (Bad Request)
- Customizer: How to Sanitize a Decimal Number Range
- PHP code printed into CSS classes
- How to properly escape in ternary operators – Wp Coding Standards?
- How to send Ajax Call from frontend without using wp_localize_script in Theme
- Get the image title and alt from custom query
- Add Text Area To Child Theme’s Home Page
- What syntax is this? “{{post.price}}”
- Theme editing “post thumbnail” help
- CSS loading as empty file in Custom Theme
- How to add Open/Close Toggle button into Twenty Fifteen theme for hiding left sidebar?
- Woocommerce variable products are being added to the card on the homepage (ajax)
- How to get php file based on URL in WordPress
- Problems with the WordPress loop [closed]
- How can I customize a WordPress theme before it’s downloaded?
- Use Custiomizer to setup meta theme color tag
- How to replace hard-coded list of custom taxonomy terms based on custom field query [closed]
- I want different post-thumbnail size depending on media size
- Rotating Header Images
- How to add thumbnails from recent posts to owl-carousel in wordpress automatically?
- Custom theme: adding unwanted tags in the content
- Where can I find the declaration of `$_wp_theme_features`?
- Calling PHP function doesn’t work in index.php
- Click to expand menu
- Relative post date in recent posts list
- Create a custom theme-specific page, invisible in the admin-panel?
- Trying send mail from Theme page
- Not able to get the exact URL of the file on server located in active wordpress theme
- Display tab title as ‘blog tagline | blog title’
- Extend the WP_Customize_Image_Control class to change its non-frame $button_labels
- A Customizer checkbox control that sets the setting to “” or to “blank” and show() or hide() a color control
- My customizer’s setting doesn’t set to the default and needed to click the control’s “Default” button before it’ll be set
- Save the outputted image into the Media Library, with a different filename and extension
- WordPress get_post_meta issue
- How to correctly add the featured immage (thumbnails) support in a WP theme? Why I can’t add a featured immage?
- WP Knowledge Base Theme bug – Subcategories and Articles, Need to change WP_Query
- Image not displayed
- append links with unique number string
- need help to arabic text in wordpress
- WP multisite network plugin fails to see classes loaded with spl autoload
- Listing current pages subp page in list items in the sidebar
- WordPress causing all code to be displayed on line 1. Receiving multiple errors after cleaning cookies and cache
- How to add button to top of theme customizer?
- Not able to call class in NHP framework
- Adding code to the function file
- Adding Media button to only pages
- Trying to display ads on only a third of posts in a loop. Keep getting Error 500?
- Sidebar doesnt get updated?
- Shortcode working in page.php but not in category.php in wordpress
- Adding a Sub Menu Options Page WP Backend Menu. Whats wrong with my code?
- How to implement conditional PHP in Footer
- Add CSS class to PHP Statement
- How to edit the default database of WordPress [closed]
- Adding php within a return statement [closed]
- How can a ‘scripts’ directory be hooked into wp_head();?
- ACF | WooCommerce | Theme Development | How to include a /template-part/ that makes use of ACF’s on a custom WooCommerce homepage?
- I need a button to appear when not loged in and another when logged in, I need help fixing code PLEASE!
- Load slideshow.css file only if Slideshow is checked / on
- Predefine Magazine Style Layouts
- Using is home to get content [closed]
- How I can give a php page a style from my theme
- WP and Laravel integration (Updated) [closed]
- Twig+WordPress how to use array arguments inside a function?
- Pagination not working properly
- paginate_links() returns NULL instead of the pagination links, but pagination is actually happening
- $_html is empty when var dumped
- How can I fix my pagination?
- How to store the_post_thumbnail() value in a variable
- Headers already sent error with get_template_part in REST API call
- PHP warning: Undefined array key 2 in feed.php
- How to hide header and footer from page template
- How to customize content-video.php file in wordpress as is in html file?
- Variable ++ in query loop
- Social Sharing Buttons in Recent Articles List
- orderby property of query on pre_get_posts returns incorrect value
- Add default block css when the block has been added with php?
- Woocommerce Convert existing order to the cart
- WordPress PHP8 Critical Error in class-wp-widget.php
- functions.php doesn’t load all custom theme assets