添加标签分类到wp_query搜索wordpress自定义帖子类型

时间:2015-08-24 07:19:37

标签: php ajax wordpress taxonomy wp-query

我有一个wordpress ajax搜索字段,它查询2个自定义帖子类型,产品和教程。我希望能够为帖子类型添加标签,并根据搜索到的标签获得字段结果。

add_action('wp_ajax_custom-search', 'search_ajax');
add_action('wp_ajax_nopriv_custom-search', 'search_ajax');
function search_ajax() {
  if($_GET['s'] === '') {
    wp_send_json(array(
      'results' => [],
    ));
  }
  else {
    $query = new WP_Query( array('s' => $_GET['s'], 'post_type' => array('product', 'tutorial'), 'tag' => '') );
    wp_send_json(array('results' => array_map(function($p){
      return array('title'=>get_the_title($p),'uri'=>get_permalink($p),'category'=>get_the_category($p->ID, 'search_category'),'postType'=>get_post_type($p),'thumb'=>get_the_post_thumbnail($p->ID,'medium'));
    }, $query->posts)));
  }   
}

0 个答案:

没有答案