WP_Query returning no results

时间:2015-06-13 作者:Reddo

我有以下代码来合并3个不同的查询:

<div class="features-list">
            <?php

                $args1 = array (
            \'post_type\' => \'page\',
            \'post__not_in\' => array(2, 48, 50, 52, 54, 56, 61, 116, 129, 58665, 58666, 58667, 58668, 57800, 62883, 74802, 75597, 75599, 75601, 75609),
            \'orderby\' => \'date\',
            \'order\' => \'DESC\',
                    \'meta_key\' => \'_wp_page_template\',
                    \'meta_value\' => \'template-features.php\'
                );
            $query1 = new WP_Query( $args1 );

            // If there are results, then push the IDs into an array
            $query1_posts = array();
            if ( $query1->have_posts() ) { 
              while( $query1->have_posts() ) {
                $query1->the_post();
                array_push( $query1_posts, get_the_ID() );
              }
            }
            wp_reset_postdata();

                $args2 = array (
            \'post_type\' => \'page\',
            \'orderby\' => \'date\',
            \'order\' => \'DESC\',
            \'meta_key\' => \'feature_type\',
            \'meta_value\' => \'guide\'
                );
            $query2 = new WP_Query( $args2 );

            // If there are results, then push the IDs into an array
            $query2_posts = array();
            if ( $query2->have_posts() ) { 
              while( $query2->have_posts() ) {
                $query2->the_post();
                array_push( $query2_posts, get_the_ID() );
              }
            }
            wp_reset_postdata();

                $args3 = array (
            \'post_type\' => \'new_features\',
            \'orderby\' => \'date\',
            \'order\' => \'DESC\',
            \'meta_key\' => \'feature_type\',
            \'meta_value\' => \'guide\'
                );
            $query3 = new WP_Query( $args3 );

            // If there are results, then push the IDs into an array
            $query3_posts = array();
            if ( $query3->have_posts() ) { 
              while( $query3->have_posts() ) {
                $query3->the_post();
                array_push( $query3_posts, get_the_ID() );
              }
            }
            wp_reset_postdata();

            $results = array_unique ( array_merge( $query1_posts, $query2_posts, $query3_posts ) );

            // WP_Query arguments
            $the_args = array (
              \'post__in\' => $results
            );

            // The Query
            $the_query = new WP_Query( $the_args );
                $count = 1;

                if ( $the_query->have_posts() ) { 
                while ($the_query -> have_posts()) {
                    $the_query -> the_post();
            ?>
            <article class="<?php if ($count == 1) { ?>g1_2<? } else { ?>g1_4<?php } ?>">
                <div class="feature_image">
                    <a href="<?php the_permalink(); ?>"><?php if ($count == 1) { the_post_thumbnail(\'full\'); } else { the_post_thumbnail(\'thumbnail\'); } ?></a>
                    <div class="feature_title"><a href="<?php the_permalink(); ?>"><?php the_title();?></a></div>
                </div>
                <p><?php the_excerpt();?></p>
            </article>
            <?php
                        $count++;
                    }
                }
                wp_reset_postdata();
            ?>
        </div><!-- .features-list -->
我知道$results具有我需要的所有post id,但$the\\u查询仍然没有返回post。

我错过了什么。到目前为止,我至少重写了10次这段代码。

1 个回复
最合适的回答,由SO网友:Michael 整理而成

您可能还需要在中设置“post\\u type”$the_args, 以及可能的“每页帖子”;http://codex.wordpress.org/Class_Reference/WP_Query#Type_Parameters

示例:

    // WP_Query arguments
    $the_args = array (
      \'post__in\' => $results,
  \'post_type\' => array( \'any\' ),
  \'posts_per_page\' => 10
    );

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post