2017-04-25 94 views
0
SELECT 
    IF (POSITION('Villa' IN p.post_title)>=1, 
     SUBSTRING(p.post_title, 7), post_title) AS 'Title', 
    p.post_title, 
    p.ID, 
    p.post_content 
FROM wp_posts p INNER JOIN wp_term_relationships tr 
ON p.ID=tr.object_id where tr.term_taxonomy_id=4 
    and p.post_status='publish' 
ORDER BY Title ASC; 

我可以运行上面的查询,以获得与“WPDB”功能像如何使用此自定义MySQL查询作为Wp_query对象检索数据?

$wpdb->get_results($query); 

但我需要返回的Wp_query对象上面的结果,因为我想使用的功能,如get_the_excerpt()

数据

回答

0

orderby参数接受post_in排序值作为对返回的帖子进行排序的可能方式。将orderby设置为post_in后,帖子将按照它们输入post_in参数的顺序返回。

下面的代码

args = [ 
     'post__in' =>[3, 1, 2], 
     'orderby' => 'post__in' 
     ]; 

$q = new WP_Query($args); 

将返回岗位1,2和3按以下顺序

3,1,2 
0

试试这个代码,

$args = array(
      'post_type' => $post_type, 
      'posts_per_page' => -1, 
      'post_status'=>'publish', 
      'orderby'=>'title', 
      'order' => 'ASC', 
      'tax_query' => array(
       array(
        'taxonomy' => $taxonomy, 
        'field' => 'id', 
        'terms' => 4 
       ) 
      ) 
     ); 
     $query = new WP_Query($args); 
     $query_posts = $query->posts; 
     var_dump($query_posts); 
0

你可以把你的WPDB结果并使用这样的帖子功能...

global $wpdb; 
global $post; 

$query = "SELECT 
    IF (POSITION('Villa' IN p.post_title)>=1, 
     SUBSTRING(p.post_title, 7), post_title) AS 'Title', 
    p.post_title, 
    p.ID, 
    p.post_content 
FROM wp_posts p INNER JOIN wp_term_relationships tr 
ON p.ID=tr.object_id where tr.term_taxonomy_id=4 
    and p.post_status='publish' 
ORDER BY Title ASC;" 

$result = $wpdb->get_results($query); 

foreach($result as $post) 
{ 
    setup_postdata($post); 
    echo "<div>" . get_the_excerpt() . "</div>"; 
} 

确保您设置$ post全局。

相关问题