2017-06-20 127 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); 
相关问题