簡體   English   中英

WordPress無法獲得所有尊重標簽和類別條件的帖子

[英]WordPress Can't get all the posts which respects tags and categories conditions

我創建了一個自定義類型的帖子,我想獲取類別和標簽中的所有帖子。 不幸的是,我不知道為什么它只返回一篇文章(我應該至少有3或4)

這是我的代碼:

$tag = $_GET["tag"];
$cat = $_GET["cat"];

$args = array(
    'post_type' => 'post_product',
    'post_status' => 'publish',
    'numberposts' => -1,
    array(
        array(
            'taxonomy' => 'cating'
        ),
        array(
            'taxonomy' => 'tagging')
    )
);

$custom_query = new WP_Query( $args );

if ( $custom_query->have_posts() ):

    while ( $custom_query->have_posts() ) :
        $custom_query->the_post();

        $product_terms = wp_get_object_terms(get_the_ID(), 'cating');
        $product_terms_tag = wp_get_object_terms(get_the_ID(), 'tagging');

        if(!empty($product_terms) && !empty($product_terms_tag)){
            if(!is_wp_error( $product_terms ) && !is_wp_error( $product_terms_tag )){

                foreach($product_terms as $term){
                    if(strcmp($term->name, $cat) == 0) {

                        foreach($product_terms_tag as $term_tag){
                            if(strcmp($term_tag->name, $tag) == 0) {

                                // display here

                            }
                        }
                    }
                }
            }
        }
    }
}

有誰知道為什么我只得到一個帖子,或者什么也沒有,因為他們是尊重條件的帖子。

謝謝。

您的分類查詢參數格式不正確。 而且您也沒有使用任何$_GET變量。

假設您通過$_GET獲取術語ID,應該是這樣的:

$args = array(
    'post_type' => 'post_product',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'tax_query' => array(
        'relation' => 'AND',
         array(
            'taxonomy' => 'tagging',
            'field' => 'id',
            'terms' => (int) $tag
        ),
        array(
            'taxonomy' => 'cating',
            'field' => 'id',
            'terms' => (int) $cat
        )
    )
);

將$ post_count與所需的帖子數添加到$ args數組中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM