Php 如何在wordpress中按内容获取帖子

Php 如何在wordpress中按内容获取帖子,php,wordpress,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(); } } els

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

你这样做吗

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

可能属于Maybe,但这里有超过32472个wordpress标签问题,那里只有33293个,同样可以说它和SONope上的家一样,不起作用。。。但是我使用自定义查询使其工作,并且我提供了
WP\u query
,但如果您的问题通过自定义查询解决,那么最好使用它:)