几种帖子类型的分类相同:如何在特定的帖子类型中隐藏空?

时间:2016-08-26 作者:pixeline

我有两种不同的自定义帖子类型(“项目”和“产品”)的分类“艺术家”。

我需要显示“艺术家”列表,但前提是他们有相关的“产品”,并且这些产品仍在库存中。

$artists = get_terms( array(
   \'taxonomy\' => \'artist\',
   \'hide_empty\' => 1,
) );
这包括有“项目”但没有“产品”的艺术家。是否可以指定“hide\\u empty”参数应该引用哪个post类型?

编辑,这是我到目前为止的资料。

        /*
        Useful when one taxonomy applies to multiple post types
        source: http://wordpress.stackexchange.com/a/24074/82
    */
    function get_terms_by_post_type( $taxonomies, $post_types ) {

        global $wpdb;

        $query = $wpdb->prepare(
            "SELECT t.*, COUNT(*) from $wpdb->terms AS t
            INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id
            INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id
            INNER JOIN $wpdb->posts AS p ON p.ID = r.object_id
            WHERE p.post_type IN(\'%s\') AND tt.taxonomy IN(\'%s\')
            GROUP BY t.term_id",
            join( "\', \'", $post_types ),
            join( "\', \'", $taxonomies )
        );

        $results = $wpdb->get_results( $query );
        return $results;
       }

$artists = get_terms_by_post_type( array(\'artist\'), array(\'product\'));

//*
if( !empty($artists) && !is_wp_error( $artists )  ){

    // filter out out-of-stock products artists
    foreach($artists as $k=>$artist){
        $args = array(
            \'post_type\'     => \'product\',
            \'post_status\'   => \'publish\',
            \'posts_per_page\' => -1,
            \'tax_query\' => array(
                \'relation\' => \'AND\',
                array(
                    \'taxonomy\' => \'artist\',
                    \'field\' => \'id\',
                    \'terms\' => array( $artist->term_id )
                ),
                array(
                    \'key\' => \'_stock_status\',
                    \'value\' => \'instock\'
                )
            )
        );
        $query = new WP_Query($args);
        echo $artist->slug;
        pr($query->post_count);

        if($query->post_count<1){
            unset($artists[$k]);
        }
    }
}
它几乎可以工作,但它不会使产品脱销。

2 个回复
最合适的回答,由SO网友:pixeline 整理而成

多亏了这个answer, 我能看到光线。下面是如何查询与“库存中”的(woocommerce)产品相关的分类术语。它同时使用tax\\u查询(将产品与分类法关联)和meta\\u查询(过滤缺货产品)。

$artists = get_terms_by_post_type( array(\'artist\'), array(\'product\'));

if( !empty($artists) && !is_wp_error( $artists )  ){

    // filter out out-of-stock products artists
    foreach($artists as $k=>$artist){
        $args = array(
            \'post_type\'     => \'product\',
            \'post_status\'   => \'publish\',
            \'posts_per_page\' => -1,
            \'meta_query\' =>array(
                \'relation\' => \'AND\',
                array(
                    \'key\' => \'_stock_status\',
                    \'value\' => \'instock\',
                    \'compare\' => \'=\'
                )
                ,
                array(
                    \'key\' => \'_stock\',
                    \'value\' => \'0\',
                    \'compare\' => \'>\'
                )
                ,
                array(
                    \'key\' => \'_stock\',
                    \'value\' => \'\',
                    \'compare\' => \'!=\'
                )
            ),
            \'tax_query\' => array(
                \'relation\' => \'AND\',
                array(
                    \'taxonomy\' => \'artist\',
                    \'field\' => \'id\',
                    \'terms\' => array( $artist->term_id )
                ),


            )
        );

        $query = new WP_Query($args);

        if($query->post_count<1){
            unset($artists[$k]);
        }
    }
}

function get_terms_by_post_type( $taxonomies, $post_types ) {

    global $wpdb;

    $query = $wpdb->prepare(
        "SELECT t.*, COUNT(*) from $wpdb->terms AS t
        INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id
        INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id
        INNER JOIN $wpdb->posts AS p ON p.ID = r.object_id
        WHERE p.post_type IN(\'%s\') AND tt.taxonomy IN(\'%s\')
        GROUP BY t.term_id",
        join( "\', \'", $post_types ),
        join( "\', \'", $taxonomies )
    );

    $results = $wpdb->get_results( $query );

    return $results;

}

SO网友:Sjoerd Boerrigter

同一问题的另一个解决方案略短且不太复杂:

function get_terms_by_posttype($taxonomy, $postType) {
    // Get all terms that have posts
    $terms = get_terms($taxonomy, [
        \'hide_empty\' => true,
    ]);

    // Remove terms that don\'t have any posts in the current post type
    $terms = array_filter($terms, function ($term) use ($postType, $taxonomy) {
        $posts = get_posts([
            \'fields\' => \'ids\',
            \'numberposts\' => 1,
            \'post_type\' => $postType,
            \'tax_query\' => [[
                \'taxonomy\' => $taxonomy,
                \'terms\' => $term,
            ]],
        ]);

        return (count($posts) > 0);
    });

    // Return whatever we have left
    return $terms;
}

相关推荐

GET_THE_TERMS与wp_GET_POST_TERMS中的奇怪结果

我正在尝试制作一个面包屑函数,但有一个小问题。。。使用时:$categories = get_the_terms( $post->ID, \'product_cat\' ); 我得到了一个循环中使用的类别数组,等等。唯一的问题是它是按字母顺序排列的。(我希望它按层次顺序排列。)经过一番挖掘,我发现了一种替代方法,即使用wp\\u get\\u post\\u terms(),但我肯定遗漏了一些东西,因为当我使用此方法时:$categories = wp_get_post_terms( $p