我正试图通过wordpress中的内容发帖,但看起来我得到一些与查询无关的随机结果。有任何想法吗 ?
$q='my query';
$query = new WP_Query('s = "'.$q.'"');
$gids = array();
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$gids[]=get_the_ID();
}
} else {
// no posts found
}
wp_reset_postdata();
//
if (count($gids)>0){
//find random one
$rand = array_rand($gids);
$post_id=$gids[$rand];
$post = get_post($post_id);
header('application/json');
echo json_encode($post);
}
答案 0 :(得分:0)
你做这样的事情
$args = array(
'post_content' => 'test',
'paged' => '1',
's' => 'post',
);
$query = new WP_Query($args);
$gids = array();
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$gids[]=get_the_ID();
}
} else {
// no posts found
}
wp_reset_postdata();
//
if (count($gids)>0){
//find random one
$rand = array_rand($gids);
$post_id=$gids[$rand];
$post = get_post($post_id);
header('application/json');
echo json_encode($post);
}