簡體   English   中英

WP_Query cpt分類法未加載帖子

[英]WP_Query cpt taxonomy not loading posts

我通過custom post type UI插件創建了一些分類法和post_types。 我將帶有表格的taxonomy_id傳遞給頁面,並且我正確地接收了它[ var_dump($_POST) ]顯示了數字示例30 我想顯示來自該自定義帖子類型類別的帖子:我嘗試了以下代碼,但未返回任何內容。

$args = [
    'tax_query' => array(
        array(
            'taxonomy' => 'school_type',
            'field' => 'term_id',
            'terms' => array('30','22'),
            // 'operator' =>  'IN'
        ),
    'post_type' =>  'school',
    )
];
if($q->have_posts()):
    while($q->have_posts()){
        $q->the_post();
        echo the_title();
    }
else:
    echo 'nothing';
endif;

誰能幫我?

你有post_typetax_query陣列,再加上你的目標$q當它不存在(不,我可以看到。反正)。

試試這樣的東西:

$args = array(
    'post_type' => 'school',
    'tax_query'     => array(
        array(
            'taxonomy'      => 'school_type',
            'field'         => 'term_id',
            'terms'         => array('30','22'),
        ),
    ),
    'numberposts'  => -1
);
$q = new WP_Query($args);
if($q->have_posts()):
    while($q->have_posts()){
        $q->the_post();
        echo the_title();
    }
else:
    echo 'nothing';
endif;

您應該具有這樣的$ arg變量,然后使用WP_Query()對象獲取帖子。

   $args = array(
        'post_type' => 'school',
        'posts_per_page'=>30,
        'post_status' => 'publish',
        'tax_query' => array(array(
            'taxonomy' => 'school_type', 
            'field' => 'term_id', 
            'terms' => array('30','22'), 
    );

    $q = new WP_Query($args);
     if($q->have_posts()):
            while($q->have_posts()){
                $q->the_post();
                echo the_title();
            }
        else:
            echo 'nothing';
        endif;

$q = new WP_Query( $args ); 在if行之前

將您的永久鏈接更改為發布類型並更新永久鏈接,希望這會有所幫助。

暫無
暫無

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

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