首页 文章

如何使用两个分类条件过滤wordpress select查询?

提问于
浏览
0

我想根据两个分类条件从wordpress数据库中选择记录 . 我有以下查询 . 但它列出了所有记录 . 我想用分类法食谱类别和术语90过滤以下查询 . 我怎么能这样做?

选择DISTINCT p.ID,p.post_author,p.post_date作为dte,wt.term_id来自wp_posts p left JOIN wp_postmeta m1 ON p.ID = m1.post_id left JOIN wp_term_relationships wtr ON(p.ID = wtr.object_id)left JOIN wp_term_taxonomy wtt ON(wtr.term_taxonomy_id = wtt.term_taxonomy_id)离开JOIN wp_terms wt ON(wt.term_id = wtt.term_id)其中p.post_type ='recipe'和p.post_status ='publish'AND(wtt.taxonomy =' recipe-cuisine'和wtt.term_id IN(17))由dte DESC命令

2 回答

  • 0

    为什么不使用WP_QUERY获取与分类法相关的帖子( recipe-category )和 recipe-category id 90 的术语

    $taxonomy       = 'recipe-category';
    $taxonomy_terms = 90;
    $args           = array(
        'tax_query' => array(
            array(
                'taxonomy' => $taxonomy,
                'field'    => 'id',
                'terms'    => $taxonomy_terms,
            ),
        ),
    );
    $posts_related_to_taxonomy      = new WP_Query($args);
    if( $posts_related_to_taxonomy->have_posts() ) :
        echo '<p>';
        while($posts_related_to_taxonomy->have_posts()) : $posts_related_to_taxonomy->the_post();
            ?>
            <a href="<?php the_permalink(); ?>">
                <span><?php the_title(); ?></span> : <?php echo get_the_excerpt(); ?>
            </a>
            <br>
        <?php endwhile;
        echo '</p>';
    else :
        echo wpautop('No Records found');
    endif;
    

    如果要在两个分类法之间进行搜索,请使用以下内容替换 tax_query 参数

    'terms' => array( $term) 此参数可以与term id或string数组一起使用,将 taxonomy1taxonomy2 替换为其他分类法slug

    'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'taxonomy1',
                'field' => 'id',
                'terms' => array( $term) 
            ),
            array(
                'taxonomy' => 'taxonomy2',
                'field' => 'id',
                'terms' => array( $term2),
            )),
    
  • 0

    好吧,更简单的方法是使用WP的get_posts template function . 看起来像参数类别是你需要的 . AFAIK允许使用任何分类术语ID,而不仅仅是类别 .

    如果您仍想使用直接的SQL查询 - 如果您以更可读的方式格式化有问题的查询,则会更容易得到答案 .

相关问题