I'm calling a "loop handler" kind of php file that opens a loop with WP_Query.

Inside the foreach loop I want to use the show_postdata function. I'm aware of the global $post aspect of this function. I tried placing the global $post everywhere in that php file without success though.

I get an error 500 server error with noch detailed error message in the catch part.

When I use the class to get data on the server inside a template it works.

This is my AJAX call:

    type       : "GET",
    data       : {numPosts : numPosts, postTypes : postTypes, template : template, postID : null, filterArgs : filterArgs, fields: fields, inclMeta : inclMeta, imgFields : imgFields },
    dataType   : "html",
    url        : bfApp.setWPUrl + "/bfLoopHandler.php",
    beforeSend : function(){
    success    : function(data){

        if (typeof(callbackSuccess) == "function") {

    error     : function(jqXHR, textStatus, errorThrown) {

        if (typeof(callbackError) == "function") {
            callbackError(jqXHR, textStatus, errorThrown);


This is an excerpt from the php file up to the point where show_postdata is called:

class bfLoop {
public function getData($postIDs, $postTypes, $numPosts, $filterArgs, $template, $fields, $inclMeta, $imgFields, $inclCats) {

    global $post;

    define('WP_USE_THEMES', false);
    $parse_uri = explode( 'wp-content', $_SERVER['SCRIPT_FILENAME'] );
    require_once( str_replace('index.php', '', $parse_uri[0]) . 'wp-load.php' );

    // Argumente setzen
    if ($postIDs == null || $postIDs == 0) {
        $args = array( 'post_type' => $postTypes, 
                       'post_status' => array('publish'),
                       'posts_per_page' => $numPosts, 
                       'meta_query' => $filterArgs,
                       'fields' => $idsOnly );
    } else {
        if (is_array($postIDs)) {
            $args = array(
            'post__in' => $postIDs,
            'post_type' => 'any');                  
        } else {
            $args = array(
            'p' => $postIDs,
            'post_type' => 'any');                

    $loop = new WP_Query( $args );  

        $retArray = array();

        $posts = $loop->get_posts();   
        foreach ( $posts as $post ) {

            try {
               setup_postdata( $post ); 
            } catch(Exception $e) {
                //return "AFTER";
                return "W:" . $e->getMessage();
            } ......
  • 1
    Don't make AJAX calls like that in WordPress. The correct way to make AJAX calls within WordPress is here: codex.wordpress.org/AJAX_in_Plugins Apr 30, 2014 at 13:46
  • I know. This is existing code i have to work with. Is there a way regardless the "wrong" approach? Apr 30, 2014 at 13:54
  • Can you indicate in the code where it's not working? I think you'd still have better luck refactoring this into the correct method. You'll spend less time, the code will work right in the proper WP context, and it'll be right when you are done. Apr 30, 2014 at 14:19
  • The code works exactly up to the line with the show_postdata command. I have checked this with a sample return value. When using this class without AJAX it also works (instantiated inside a template). You might be right, but the idea to have the right line just at the right place is also interesting ;-) the whole theme I have here is not using WP typical architecture so its difficult to find the "starting point" of refactoring... Apr 30, 2014 at 14:30
  • So this is not a question anymore?
    – fuxia
    Apr 30, 2014 at 16:02

1 Answer 1


Solved by getting author, post_date, permalink & excerpt manually without setup_postdata:

        $excerpt = myCustomExcerptFunction($post, 35);
        $post->post_excerpt = $excerpt;

        // Permalinks
        $post->post_permalink = get_post_permalink($post->ID);

        // Author
        $authorObjekt = get_userdata($post->post_author);
        $post->post_author_displayname = $authorObjekt->data->display_name;
        $post->post_author_nicename = $authorObjekt->data->user_nicename;

        // Date
        $post->post_date_wpformat = date_i18n( get_option( 'date_format' ), 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.