0

我正在尝试在 wordpress 中按内容发布,但看起来我得到了一些未与 query 链接的随机结果。有任何想法吗 ?

$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);
 }
4

1 回答 1

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);
 }
于 2013-06-26T13:00:41.780 回答