Support » Plugins » setup_postdata problem

  • Hi,
    I’m trying to get a custom loop written using setup_postdata
    but the functions like the_title() are returning nothing.

    I’ve set up a fresh install of wp 2.3.2 to ensure that no other plugins or theme issues are causing it.

    And I’ve paired the code down to the minimum to confirm the issue

    Here is the resultant code:

    add_management_page("LIST POSTS", "LIST POSTS", 9, basename(__FILE__), MYPLUGIN_show_management_page); 
    
    function MYPLUGIN_show_management_page() {
    	global $wpdb;
    
    	$sql = "SELECT $wpdb->posts.* FROM $wpdb->posts LIMIT 0,10";
    	$pageposts = $wpdb->get_results($sql);
     	?>
     	<div class="wrap">
    	<?php
    	if ($pageposts) {
    		$bgcolor = '';
    		foreach ($pageposts as $pagepost) {
    			setup_postdata($pagepost);
    
    			echo "<hr>";
    			echo "<br>ID: " . the_ID();
    			echo "<br>Title: " . the_title();
    			echo "<br>Date: " . the_time(__('Y-m-d \<\b\r \/\> g:i:s a'));
    			echo "<br>Categories: " . the_category(',');
    			echo "<br>Author: " . the_author();
    
    		}
    	}
    	?>
    	</div>
    	<?
    }

    What I end up with is:

    1
    ID:
    Title:
    Date: Uncategorized
    Categories: admin
    Author: admin2
    
    ID:
    Title:
    Date: Uncategorized
    Categories: admin
    Author: admin

    Any help in getting these functions to work properly would be greatly, greatly appreciated.

    Regards,
    Andy

Viewing 6 replies - 1 through 6 (of 6 total)
  • I am attempting to use setup_postdata as well. It looks like it’s not quite rock solid. I took a more hamhanded approach, and it seems to work ok.

    // My new post is stored in $pagepost
    
    $stored_post = clone $GLOBALS['post'];
    $GLOBALS['post'] = $pagepost;
    setup_postdata($pagepost);
    
    // Now I can use all my functions, including the_title(), etc.
    // When I'm done...
    
    $GLOBALS['post'] = clone $stored_post;

    No idea why this works, but it just seems that either setup_postdata doesn’t get all the elements in the right place or the other template functions (e.g., the_title(), etc.) seem to ignore what setup_postdata does.

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    setup_postdata() doesn’t set the global $post variable, which some of the template functions use directly.

    This is why you should use the_post() instead. setup_postdata() was never really meant to be called directly.

    Of course if WordPress was properly documented, everyone would know this.

    Very true. The documentation is a wiki, meaning that you can help write it. Why not click on “docs” at the top of this page and add this information?

    <?php $issue_articles = $wpdb->get_results("
    	SELECT * FROM $wpdb->posts
    	LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
    	WHERE $wpdb->posts.post_status = 'publish'
    	AND $wpdb->postmeta.meta_key = 'magazine'
    	AND $wpdb->postmeta.meta_value = $display_issue
    	ORDER BY $wpdb->posts.post_date DESC
    	", OBJECT); ?>
    <?php foreach($issue_articles as $issue_article) : the_post($issue_article); ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_ID(); ?></a></li>
    <?php endforeach; ?>
    </ul>

    This code returns the ID’s of the posts that would regularly be displayed, not the ones from the custom query. However when I add that suspicious, heavy handed code all of a sudden the posts from the custom query are getting displayed.

    I don’t want to complain, but I just wish I knew what was going on.

    Hi andrewpeacock,

    You need to declare :

    global $post;

    after global $wpdb; in your code.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘setup_postdata problem’ is closed to new replies.