WordPress.org

Ready to get started?Download WordPress

Forums

setup_postdata problem (7 posts)

  1. andrewpeacock
    Member
    Posted 6 years ago #

    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

  2. almostdaniel
    Member
    Posted 6 years ago #

    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.

  3. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    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.

  4. davestewart
    Member
    Posted 6 years ago #

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

  5. Adam Brown
    Member
    Posted 6 years ago #

    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?

  6. jamiefehr
    Member
    Posted 6 years ago #

    <?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.

  7. magneto.me
    Member
    Posted 5 years ago #

    Hi andrewpeacock,

    You need to declare :

    global $post;

    after global $wpdb; in your code.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.