Where is your code? In a function? Somewhere in a template?
setup_postdata
does not replace the global $post
with the $post
object passed in. It does, however, modify some other globals.
So if you’re trying to use the various the_*
template tags, you need to explicitly replace the global $post
object on which they depend.
In a function, for example…
<?php
function wpse73103_loop_example()
{
global $post; // this is important!
$posts = new WP_Query(/* your args here */);
// because $post is global above, this will
// overwrite the global object.
foreach($posts as $post)
{
setup_postdata($post);
// do stuff
}
wp_reset_postdata(); // back to normal
}
Anyway, the moral of the story is that you should try putting global $post;
before any of the code you posted (which is very much correct).
Related Posts:
- How to maintain W3C standards compliance of a theme
- Load plugin on specific page only
- Does a plugin’s “main” file need to be named the same as the folder containing it?
- Disable plugins on cron and ajax page
- Convert WordPress pages to PDF
- Plugin or mod for wordpress to make content publication SUPER easy
- enqueue script only if it is not already enqueue
- Display (and manage) webcam images?
- Overwrite textdomain of plugins by default – Lost Translation
- jQuery plugin only in one page/post
- Google Analytics post views in WordPress? [closed]
- Custom database table for plugin not creating on activation
- Hook (upgrader_process_complete) running moment
- WordPress 3.1 and Disqus throws Warning: number_format() error in Posts List
- Is there a quick way to inject i18n domain into theme/plugin files?
- Can’t see login page after migration
- How to import 55k images (uploaded via FTP) into WordPress Media libary? [closed]
- Debugging Technique Question re: functions.php
- How can I list recent posts excerpts?
- Custom Taxonomy Tag Search
- Retrieving plugin options value saved through setting
- An issue with SEO Ultimate
- How do I modify the position of a plugin?
- Hiding posts – WP Hide Post not working
- Add a custom form on the post-new.php admin page
- how to give a user a role?
- Scripts at the end of the page
- WSOD after upgrading to 3.1
- looking for navigation plugin (accordion) [closed]
- When using Simple Fields plugin, how do I pull the information out of the database to display on a page?
- Trouble with editing template for “List category posts” plugin
- How to use JSON data from URL into table on my wordpress website
- How to show WP addon installation path during installation?
- Replace default og:image tag YOAST
- Automatic plugin updates not working
- Fromcraft Plugin On click submit button [closed]
- A include_template Question
- PHP Fatal error: Call to a member function locale() on a non-object
- WordPress error: Options page Setting_ not found in the allowed options list
- Shortcode Placeholder Plugin
- Minimizing scripts & css files that plugins add
- How to setup a product that has multiple price options? [closed]
- flush_rewrite_rules() not working with update_option_{$option}
- correctness of URL
- How to ignore extra whitespaces in translation functions like _e?
- Can backlinks to my site increase my chances of plugin rejection
- Hide a certain category name from Related Posts
- Price comparison table based on Custom Post Type?
- The “Child Pages Shortcode” plugin, block template
- How do I determine if the user who registered is not spam?
- display unique post per category
- Is there a way to insert multiple images into a post at the same time without using a gallery?
- How to get a custom page through plugin?
- Filters on the_content with plugins, pages and shortcode
- Plugin to send messages to members/lists but not full-blown newsletters
- Plugin access and Roles
- How to create a plugin page that shows XML?
- Plugin prevents code from working
- Determining Whether Automatic Upgrade will Fail?
- How to make WooCommerce payment method field optional?
- can’t install any wp plugins [duplicate]
- Show an admin menu item in Editor role
- Add Plugin broken after removing Booster for Woocommerce
- WP_CONTENT_DIR disables plugin directory
- WordPress Custom Hook with Class method
- How to display the custom post related blog by category?
- Overriding inline !important CSS with Javscript code snippet with media query
- WP Forms not displaying,becomming an fatal error
- Undefined index: custom_post_template_present
- Ensure WordPress Theme Uninstalls Completely
- Image path in childs theme
- Using foreach loop for `$instance` of form and update array widget iteration
- Login issue in WordPress
- Mobile and Tablet responsive in Elementor but not on the actual site?
- WordPress backend post update without refresh
- Warning: sprintf(): Too few arguments in nav-menu-template.php
- how to insert thousands of posts faster?
- Redeclare a plugins function/class in my theme?
- RSS icons have the same Alt tag. Lighthouse is failing these elements. How do I display different alt tags for each icon?
- Coupon Codes for product discount combined with conditional free shipping
- How to fix slow comment query?
- wp meta_query meta_value compare =REGEXP delimited by space
- Sending post data over REST API, how to parse shortcodes in post_content?
- Primary Menu doesn’t show because of w3 cache
- How to insert variable which contain array value in wp_options table?
- Wp Ecommerce Reposition Product Page Product Thumbnail Image
- Array to string conversion error when trying to POST multidimensional array
- How to test a WordPress plugin on a single post?
- WordPress Permission Problems on Ubuntu 12.04 with LAMP stack
- How to Overwrite validate_plugin function
- Plugin and javascript placement
- Disable plugin function
- WordPress plugin WP-blocks is adding slashes before apostrophe
- Image rotator plugin
- Adding Category in URL in some post
- Why does theme need woocommerce to show images / content on front page?
- WordPress Hooks : Where to place callbacks that repetitively yield the same effect?
- wordpress Ajax success doesn’t return the value
- How to create different woocommerce single_product.php pages for each product? [closed]
- ShipHub for WordPress