2017-06-20 129 views
0

您好,我需要使用類似運算符按類別ID和帖子標題獲取帖子類型的結果。根據自定義分類標識列出自定義帖子類型,並按帖子編號過濾ID

例如

Category name : Sports 
Post name : Basketball , hockey , volley ball 

從下面的查詢,我得到職位名稱及其編號,我把它們傳遞給了get後查詢,但查詢返回類別

對於想我搜索裏面的所有職位對於那個類別內的排球,我得到了所有結果,我只需要輸出中的排球。

查看我的代碼下面

$ mypostids_title = $ wpdb-> get_col( 「選擇ID從$ wpdb->訊息其中POST_TITLE LIKE '%$標題%'」);

$args_sby= array(
    'post_type'  => 'campaign', 
    'post_status' => 'publish', 
    'posts_per_page' => -1,  
    'post__in' => $mypostids_title, 
     'tax_query' => array(
      array(
      'taxonomy' => 'campaign_category', 
      'field' => 'term_id',      
      'terms' => $_GET['c'], 
      'operator' => 'AND',     
      ) 
     ), 

    ); 
$posts = get_posts($args_sby); 

我從上面的select查詢得到的文章ID的數組,我通過他們的get後的查詢參數內,我只得到過分類的結果,但我需要得到的分類以及後ID結果,請讓我知道如何解決這個問題。 在此先感謝。

+0

In $ _GET ['c']變量你得到了什麼?術語ID或術語slug? –

回答

1
$custom_terms = get_terms('custom_taxonomy'); 

foreach($custom_terms as $custom_term) { 
    wp_reset_query(); 
    $args = array('post_type' => 'custom_post_type', 
     'tax_query' => array(
      array(
       'taxonomy' => 'custom_taxonomy', 
       'field' => 'slug', 
       'terms' => $custom_term->slug, 
      ), 
     ), 
    ); 

    $loop = new WP_Query($args); 
    if($loop->have_posts()) { 
     echo '<h2>'.$custom_term->name.'</h2>'; 

     while($loop->have_posts()) : $loop->the_post(); 
      echo '<a href="'.get_permalink().'">'.get_the_title().'</a><br>'; 
     endwhile; 
    } 

你可以試試這個代碼

+0

嗨感謝您的答案,但上述將返回該類別內的所有帖子,我需要在該類別內獲得特定的帖子集。像類別體育運動 – ManoharSingh

+1

內的名稱排球帖子標題,你可以試試這個 –

1

嘗試下面的代碼,它可能是有幫助的。您可以直接在query_posts中傳遞您的搜索關鍵字和類別。相關

$args_sby= array(
    'post_type'  => 'campaign', 
    'post_status' => 'publish', 
    'posts_per_page' => -1,  
    's' => $keywords, 
    'taxonomy' => 'campaign_category', 
    'term' => 'yourterm' 
    ); 
$posts = query_posts($args_sby); 

查找參數幫助here

1

嘗試this.It力量幫助你。

$args_sby= array(
    'post_type'  => 'campaign', 
    'post_status' => 'publish', 
    'posts_per_page' => -1, 
    'post_title'  => $title, 
    'tax_query' => array(
     array(
      'taxonomy' => 'campaign_category', 
      'field' => 'term_slug', 
      'terms' => $_GET['c'], 
      'operator' => 'AND', 
     ) 
    ), 
); 
$posts = get_posts($args_sby); 

之後請把下面的代碼放到你主題的function.php文件中。

//Filter for post title. 
function title_filter($where, &$wp_query){ 
    global $wpdb; 
    if ($search_term = $wp_query->get('post_title')) { 
     $where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . $wpdb->esc_like($search_term) . '%\''; 
    }  
    return $where; 
} 

add_filter('posts_where', 'title_filter', 10, 2); 
相關問題