WordPress:如何使用"wp_query"类搜索"post_content"的帖子?

Don*_*ant 5 wordpress

正如标题所示,我正试图通过它找到一个帖子post_content.我该怎么做?谢谢

例: SELECT * FROM DBname WHERE post_content LIKE '%phrase%'

max*_*eni 10

或者,(并且因为它已被指定)你可以通过实际使用"WP_Query"类来实现这一点:

// The Query
$my_query = new WP_Query( array(
    'post_type' => 'post',
    's' => 'phrase'
));

// The Loop
while ( $my_query->have_posts() ) {
    $my_query->the_post();
    get_content();
    //...
}
Run Code Online (Sandbox Code Playgroud)

  • 不幸的是,这并不完全正确,因为您也可以通过这种方式搜索post_title和post_excerpt:`$ search。= $ wpdb-> prepare(“ {$ searchand}(({{$ wpdb-> posts} .post_title $ like_op%s )$ andor_op({$ wpdb-> posts} .post_excerpt $ like_op%s)$ andor_op({$ wpdb-> posts} .post_content $ like_op%s))“,$ like,$ like,$ like);` (2认同)

Rya*_*mpt 6

像这样的东西?

$result = $wpdb->get_results( "SELECT * FROM wp_posts WHERE post_content LIKE '%phrase%'" );
  if ($result){
    foreach($result as $pageThing){
      echo $pageThing->post_content;
    }
  }
Run Code Online (Sandbox Code Playgroud)