【问题标题】:List custom post type by custom taxonomy id and filter by array on post id按自定义分类 id 列出自定义帖子类型并按帖子 id 上的数组过滤
【发布时间】:2017-06-20 13:30:09
【问题描述】:

您好,我需要使用 like 运算符按类别 id 和帖子标题获取帖子类型的结果。

例如

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

从下面的查询中,我得到帖子名称及其 ID,并将它们传递给 get post 查询,但查询返回该类别内的所有帖子

假设我在该类别中搜索排球,我得到所有结果,我只需要输出中的排球。

在下面查看我的代码

$mypostids_title = $wpdb->get_col("select ID from $wpdb->posts where post_title like '%$title%'");

$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 查询中获取了 post id 数组,并将它们传递到 get post 查询参数中,我只得到分类的结果,但我需要通过分类和 post id 获取结果,请告诉我如何解决这个问题。 提前致谢。

【问题讨论】:

  • 在 $_GET['c'] 变量中你得到了什么?术语 ID 或术语 slug ?

标签: php wordpress custom-post-type


【解决方案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

【讨论】:

    【解决方案2】:
    $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;
         }
    

    你可以试试这个代码

    【讨论】:

    • 您好,感谢您的回答,但以上将返回该类别内的所有帖子,我需要获取该类别内的特定帖子集。喜欢在体育类别中名称为排球的帖子标题
    • 你可以试试这个
    【解决方案3】:

    试试这个。它可能对你有帮助。

    $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 );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-24
      • 1970-01-01
      • 1970-01-01
      • 2015-07-14
      • 2019-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多