繁体   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