我有在直播系统上运行良好的代码,我正在将其移植到一个新主题。在新主题中,WP\\u查询似乎总是返回最后一篇自定义帖子。
我的职能是:
function get_wpinfo( $entry) {
global $post;
$resp_array = array();
$args = array(
\'posts_per_page\'=> 1,
\'post_type\' => \'table_element\',
\'post_status\' => \'publish\',
\'post_name\' => $entry );
print "Args = "; var_dump( $args );
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
$query->the_post();
print_r( $post );
}
wp_reset_postdata();
return $resp_array;
}
第一次打印的输出为:
Args = array(4) {
["posts_per_page"]=> int(1)
["post_type"]=> string(13) "table_element"
["post_status"]=> string(7) "publish"
["post_name"]=> string(2) "24"
}
print\\r的输出为:
WP_Post Object
(
[ID] => 4239
[post_author] => 1
[post_date] => 2015-08-25 19:00:44
[post_date_gmt] => 2015-08-25 19:00:44
[post_content] => Source: <a href="https://www.linkedin.com/pulse/5-predictions-predictive-analytics-2015-mark-rabkin" target="“_blank">5 Analytics Predictions 2015</a>
[post_title] => 2
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => 2
[to_ping] =>
[pinged] =>
[post_modified] => 2015-08-25 19:00:44
[post_modified_gmt] => 2015-08-25 19:00:44
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://www.meshagency-salient.com.php53-17.dfw1-2.websitetestlink.com/?post_type=table_element&p=4239
[menu_order] => 0
[post_type] => table_element
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
起初,代码总是返回带有slug“99”的自定义帖子。然后我意识到,它正在返回创建的最后一篇帖子。我删除并重新创建了帖子“1”和“2”,现在它总是返回“2”。
任何想法都将不胜感激!