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:
$.ajax({
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") {
callbackSuccess(data);
}
},
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();
} ......