【问题标题】:Wordpress query with multiple variable post IDs具有多个可变帖子 ID 的 Wordpress 查询
【发布时间】:2016-07-05 17:53:17
【问题描述】:

我正在尝试创建一个自定义 wordpress 查询,该查询基于使用 Elliot Condon 出色的高级自定义字段设置的 3 个不同变量提取 3 个特定 ID。

我正在使用它来设置我的变量:

<?php
    $post1 = get_field('post_1');
    $post2 = get_field('post_2');
    $post3 = get_field('post_3');
?>

我想将这些变量传递给这样的自定义查询:

<?php
    $post_list = array($post1, $post2, $post3);
    foreach( $post_list as $post_id ) :
        query_posts('p='.$post_id);
        while (have_posts()) : the_post();
        // echo the post
        endwhile;
        wp_reset_query();
    endforeach;
?>

但是,上述方法似乎不起作用并导致页面损坏。有人对如何解决有任何想法吗?我显然将变量传递到查询中是错误的,但我不知道如何解决它。

编辑 - 解决方案

这是工作更新块。非常感谢 DACrosby!我正在运行自定义帖子类型的查询,因此我需要在 $args 中指定哪种类型。

<div class="row">
    <?php
    $post1 = get_field('related_1');
    $post2 = get_field('related_2');
    $post3 = get_field('related_3');
    $args = array(
        'post__in' => array( $post1, $post2, $post3 ),
        'post_type' => 'work'
    );
    $the_query = new WP_Query( $args );
    ?>
    <?php if ( $the_query->have_posts() ): ?>
    <ul class="case-studies cf fade-in fade-in-3">
        <!-- Basic Projects -->
        <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
            <?php
                //Get Featured Image URL
                $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
                //Get Project Categories
                $terms = get_the_terms( $post->ID, 'type' );
            ?>
            <li class="case-study">
                <img src="<?php echo $feat_image; ?>" alt="<?php the_title(); ?>">
                <a href="<?php the_permalink(); ?>" class="cs-hover">
                    <div class="col-table cs-text">
                        <div class="col-tcell">
                            <span><?php the_title(); ?></span>
                            <span class="divider"></span>
                            <span><?php the_field('description'); ?></span>
                            <?php if(get_field('services')): ?>
                                <ul class="tags">
                                    <?php while(has_sub_field('services')): ?>
                                        <li><?php the_sub_field('service'); ?></li>
                                    <?php endwhile; ?>
                                </ul>
                            <?php endif; ?>
                            <span class="text-link">View Project</span>
                        </div>
                    </div>
                </a>
            </li>
        <?php endwhile; ?>
    </ul>
    <?php endif; ?>
    <?php wp_reset_query(); ?>
</div>

【问题讨论】:

    标签: php mysql arrays wordpress


    【解决方案1】:

    From the documentation

    注意事项 query_posts() 只是众多查询数据库和生成帖子列表的方法之一。在决定使用 query_posts() 之前,请务必了解其缺点。

    改变主循环 query_posts() 用于改变主循环。 ...

    二级循环要创建二级列表(例如,页面底部的相关帖子列表,或侧边栏小部件中的链接列表),请尝试创建 WP_Query 的新实例或使用 get_posts()。

    An example of WP_Query usage:

    // The Query
    $the_query = new WP_Query( $args );
    
    // The Loop
    if ( $the_query->have_posts() ) {
      echo '<ul>';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            echo '<li>' . get_the_title() . '</li>';
        }
        echo '</ul>';
    } else {
        // no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();
    

    从那里您只需修改发送到 WP_Query 的 $args。例如:

    $args = array(
             'post__in' => array( $post1, $post2, $post3 ),
             'post_type' => 'desired_post_type'
    ); 
    

    【讨论】:

    • 我尝试实现它,但仍然无法让它工作。我在上面添加了完整的代码块......也许你能发现我哪里出错了?非常感谢您的帮助!
    • 我没有立即看到任何错误。您是否遇到任何错误?检查服务器上的 error_log 文件,或display errors through WordPress
    • 日志中没有出现任何错误,页面的其余部分加载正常。输出的只是&lt;div class="row"&gt;" "&lt;/div&gt;
    • 验证 get_field 返回的内容。我经常使用 ACF,默认情况下它会返回一个 post 对象 - 对于此设置,您希望它返回对象 ID。这是每个自定义字段的自定义字段设置部分中的设置。 var_dump($args); 的输出是什么?
    • 是的,我已将其设置为返回 ID。这是 var_dump 的结果:array(1) { ["post__in"]=&gt; array(3) { [0]=&gt; int(84) [1]=&gt; int(92) [2]=&gt; int(104) } }
    猜你喜欢
    • 2021-09-27
    • 1970-01-01
    • 2012-12-09
    • 2014-03-05
    • 1970-01-01
    • 2021-09-23
    • 2015-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多