wp查询中的未定义偏移量

时间:2017-09-05 08:39:03

标签: wordpress

虽然我的搜索结果还不错,但是我的wp查询出现了“未定义的偏移:0”错误,这很烦人:

$terms = wp_get_post_terms($post->ID, 'custom_taxonomy', array("fields" => "all"));

foreach($terms as $term) {

    $posts = get_posts(
        array(
            'posts_per_page' => -1,
            'post_type' => 'custom_post_type',
            'tax_query' => array(
                array(
                    'taxonomy' => 'custom_taxonomy',
                    'field' => 'term_id',
                    'terms' => $term->term_id,
                )
            )
        )
    );

    foreach ($posts as $post ) {

        if(count($post) < 1) {

        } else {

            echo $post->post_title;
        }
    }
}

我尝试过!empty和check count的变体,但似乎没有工作。有什么指示吗?

更新

如果我删除最后一个foreach循环,我仍然会收到未定义的偏移警告:

foreach($terms as $term) {

    $posts = get_posts(
        array(
            'posts_per_page' => -1,
            'post_type' => 'custom_post_type',
            'tax_query' => array(
                array(
                    'taxonomy' => 'custom_taxonomy',
                    'field' => 'term_id',
                    'terms' => $term->term_id,
                )
            )
        )
    );
}

0 个答案:

没有答案