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 can I make add_image_size() crop from the top?
- Delete WordPress plugin Repository
- What is right way to go about adding functionality to a published WordPress plugin?
- Add section (add_settings_section) to a custom page (add_submenu_page)
- How to remove WPML Generator Meta Tag by theme’s functions.php (override plugin function)? [closed]
- Load plugin selectively for pages or posts, etc
- Can i create two tables at single time while installation of custom plugin?
- How do I sanitize a javascript text?
- How can a Firebase user registration and login be integrated into a WordPress site?
- How to override shortcodes.php core file?
- Aggregate Summaries of Posts of Different Blogs in Multisite Instance
- check for the current screen
- WordPress PHP plugin – Settings page error
- How to fork a plugin to avoid updates after modifications?
- How to display public user profile with 2 additional fields? (GitHub source code included)
- How to show custom static content at top of each category page?
- How to show category images (from plugin) on a regular page (page-example.php)?
- echo do_shortcode is not working on theme’s template
- use function inside another function in WP Plugin
- Can you limit the memory usage of a particular plugin?
- Scope for PHP Variables Assigned in functions.php or a plugin
- Override the filter from plugin in child theme
- How to remove addthis from my default template
- Average Account Age
- Dont’ charge customer until product ships – woocommerce [closed]
- JS Support Ticket – Auto create WP account
- How do I display only the latest post on my home page, while maintaining proper plugin hooks?
- how to open page, defined in plugin, from dashboad widget
- How to remove plug in styles for WPForms Lite [closed]
- Change upload folder for a CPT
- Is there an npm library that uses best practices to scaffold a wordpress plugin that is build with React?
- Passing an array from shortcode-function to filter-function
- Plugin update failed – ‘Installation failed: 504 Gateway Time-out 504 Gateway Time-out nginx/1.15.8’ error message
- How to allow an editor to edit all WordPress blog posts but have the last say as administrator?
- WP_Async_Task doesn’t appear to be running asynchronously
- Using plugin functions/methods within templates
- How to retain table data in wordpress phpunit tests
- What happens if I don’t update my plugins?
- Hide certain widgets from specific user roles in admin
- Delay JavaScript files from loading
- remove_action not removing add_action from constructor
- How to display checked posts on another page over AJAX? (like comparasion style)
- Custom widget doesn’t save values from dropdown box
- Is there a way to make the main page only display a brief description of the full article?
- jQuery Click event is not fired at all
- Limit WP code scope into plugin
- Widget for specific category
- How to call external functions from a PHP script in a WordPress plugin?
- How to hide post from Home page..?
- Generate Own Custom CSS Button with icon
- Unable to install plugins
- Execute a function when the entire page is displayed
- Is there any way to inject text onto pages?
- WordPress as heavily personalized content management portal, and somewhat like dropbox [closed]
- “switchEditors is not defined” with wp_editor() in jQuery UI tabs
- Plugin that lets visitors Like a post (not facebook) and stores likes in custom meta?
- How to get Recent Post From Each Category with Thumbnail?
- Error Using Audio plugin
- Multisite Change from domain.com to sub.newdomain.com
- “Welcome to WordPress” Plugin Required Error
- How to list datas from database in a topic?
- Downloading a file
- Profiling the initial PHP file, anyone, that get like 10 seconds to load
- How use wp_insert_post and add og:custom tags?
- Remove Keywords From Webpage But Still Use for SEO
- How to change “Read More” text?
- Troubleshooting Admin_Notice
- Images are broken in Wysiwyg Editor – ACF backend
- How to use wp_editor and save its data in wp_posts table
- WordPress database problem wp_posts, primary key
- Auto trigger of popup
- My WordPress activation hook isn’t working
- How to allow URL with filename & extension in wordpress?
- Plugin will not write background color to site
- WordPress Plugin PHP Not Calling Function
- how to show the tables of my plugin in the export tab of WordPress?
- load plugin in code snippet
- CF7 Custom Recipient – Changing the text
- Do_shortcode before send email content
- When the user entered an unauthorized url redirect to login page
- Add Cancel Button to a Custom Meta Box
- Products aren’t detected if CSV importer are used
- Get plugin to background of page
- how to create database to use it in wordpress website?
- WordPress Feedback popup with date saved into table
- Plugin option for input box validation
- Include paths and functions for external scripts
- Postal address auto-complete on profile page
- Share Button Like SuperUser
- Couldn’t find a documentation for a filter API in plugin development
- phpBB 3 to bbPress
- What plugin(s) are best for this User registration task? [closed]
- Trying to display posts but getting the pages as output
- How to show random feed from DB and show content in my site
- Plugins not showing on dashboard?
- register_activation_hook not working
- Cannot register a block in my Twig-based theme using Timber for WP
- How to use `$order->get_discount_total()`, avoiding defoult WC decimal rounding?
- How to install and activate a plugin using an external PHP script?
- Can plugin authors make db tables remain upon deactivation, but be removed upon plugin deletion?