如何通过wordpress中的内容发布

时间:2013-06-26 12:51:39

标签: php wordpress

我正试图通过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);
 }

1 个答案:

答案 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);
 }