Wordpress 自定义WP#u查询赢得';t将项目打印到页面,即使它包含预期的数据

Wordpress 自定义WP#u查询赢得';t将项目打印到页面,即使它包含预期的数据,wordpress,wordpress-theming,Wordpress,Wordpress Theming,我有一个WP_查询,其中包含我希望在页面上循环的数据: $query_posts_for_board_game = new WP_Query(get_posts(array( 'post_type' => $mm_custom_post_types, 'numberposts' => 20, 'meta_query' => array( array( 'key' => array('board_game',

我有一个WP_查询,其中包含我希望在页面上循环的数据:

$query_posts_for_board_game = new WP_Query(get_posts(array(
    'post_type' => $mm_custom_post_types,
    'numberposts' => 20,
    'meta_query' => array(
        array(
            'key' => array('board_game', 'board_games'),
            'value' => get_the_ID(),
            'compare' => 'LIKE'
        )
    )
)));
当我对它进行var\u dump时,我可以看到它在
query
query\u vars
属性中有数据,但是当我使用
$query\u posts\u for_board\u game->have\u posts()
方法循环它时,没有输出任何内容。这段代码只是打印else块

<?php if($query_posts_for_board_game->have_posts()): ?>
    <?php while ($query_posts_for_board_game->have_posts()) : $query_posts_for_board_game->the_post(); ?>
        <?php get_template_part('template-parts/layouts/content', 'b1' ); ?>
    <?php endwhile; ?>
<?php else : ?>
    <?php get_template_part('template-parts/layouts/content-none' ); ?>
<?php endif;?>

我似乎记得这是因为我的自定义查询不是“查询”的一部分。是否有一种方法可以覆盖“查询”,以便输出我的内容?在其他项目输出后,我可以简单地将查询向下移动一步吗?

我解决了它!我有点震惊,事情竟如此简单

我保留了WP_查询,就像上面的第一个查询一样。然后在我的循环中,我简单地将迭代器的名称更改为
$post
,它就开始工作了。这是其他有此问题的人的完整代码

$query_posts_for_board_game = new WP_Query(get_posts(array(
    'post_type' => $mm_custom_post_types,
    'numberposts' => 20,
    'meta_query' => array(
        array(
            'key' => array('board_game', 'board_games'),
            'value' => get_the_ID(),
            'compare' => 'LIKE'
        )
    )
)));
$posts_for_board_games = $query_posts_for_board_game->query;
以及输出:

<?php if($posts_for_board_games): ?>
    <?php foreach ($posts_for_board_games as $post): ?>
        <?php get_template_part('template-parts/layouts/content', 'b1' ); ?>
    <?php
        endforeach;
        wp_reset_postdata();
    ?>
<?php else: ?>
    <?php get_template_part('template-parts/layouts/content-none' ); ?>
<?php endif;?>

我有点恼火,因为我不得不使用一个中间变量来获取查询内容,但我对这段代码没有意见

<?php if($posts_for_board_games): ?>
    <?php foreach ($posts_for_board_games as $post): ?>
        <?php get_template_part('template-parts/layouts/content', 'b1' ); ?>
    <?php
        endforeach;
        wp_reset_postdata();
    ?>
<?php else: ?>
    <?php get_template_part('template-parts/layouts/content-none' ); ?>
<?php endif;?>