覆盖新WP_QUERY中的POSTS_PER_PAGE

时间:2014-12-14 作者:kdev

我已经试了几个小时来解决这个问题。问题是,我有一个主题new WP_Query 设置循环。这很好用。

我想获取原始查询,修改它,然后使用另一个自定义查询来显示修改后的帖子列表。

除了\'posts_per_page\' 参数,该参数显示为正确设置(请参见下面的$args),但显示所有帖子。

这是我的代码:

目录搜索。php

    <?php
/**
 * @package WordPress
 * @subpackage Parent_Theme
 * @since Parent Theme 1.0
 *
 * Template Name: Directory Search
 */

get_header(); ?>

<section class="content" id="content">

<?php   
$i = 0;

$search_query = get_directory_search_query();

if ( $search_query->have_posts() ) : ?>

<?php                   
    // ---------------------------------------------------------------------------------
    // Clickable maps outside contained content area
    // --------------------------------------------------------------------------------- 
?>   
    <div class="bg-map">
        <?php include @ (\'includes/include-archive-map.php\'); ?>
    </div>

    <div class="container header-container">


        <!--    archive header -->
        <header class="page-header entry-header">

            <div class="header-inner">

                <h1 class="page-title">

                    <img src="<?php bloginfo( \'stylesheet_directory\' ); ?>/assets/css/images/the.png" alt="THE" class="the" />

                    SEARCH RESULTS
                </h1>
                <h2><?php echo $search_query->found_posts; ?> entrie(s) for "<?php 

                    $a = str_replace("-", " ", $a);
                    $t = str_replace("-", " ", $t);
                    switch (\'n\')
                    {
                    case ( $a !== \'\'  && $t !== \'\' && $p !== \'\' ) :
                        echo $a .\', \' . $t .\', \' . $p; break;

                    case (  $a !== \'\'  && $t !== \'\' ) :
                        echo $a .\', \' . $t; break;

                    case ( $a  !== \'\'  && $p  !== \'\' ) :
                        echo $a .\', \' . $p; break;

                    case ( $t !== \'\' && $p !== \'\' ) :
                        echo $t .\', \' . $p; break;

                    case ( $a !== \'\' ) :
                        echo $a; break;

                    case ( $t !== \'\' ) :
                        echo $t; break;

                    case ( $p !== \'\' ) :
                        echo $p; break; 

                    default :
                        echo $a .\', \' . $t .\', \' . $p; break;

                    }
                    ?>"
                </h2>


            <div class="seperator"></div> 
            </div>

        </header>
        <div class="seperator"></div>
    </div>

    <div class="content-bg">

        <div class="container">

            <div class="flexible-row">

                <div class="span8 pull-right">
                    <div class="primary " id="primary" role="main">


                        <?php
                        // ---------------------------------------------------------------------------------
                        // Start loop for main archives
                        // --------------------------------------------------------------------------------- ?>

                        <div class="flexible-row featured" id="listings">

                            <?php /* Start the Loop insde content.php so that we can count the numvber of posts */ ?>
                            <?php  ?>

                            <?php
                            global $i;
                            global $deal_count;
                            $deal_count = 0;

                            while ( $search_query->have_posts() ) : $search_query->the_post();
                            $i++;  ?>

                                <?php get_template_part(\'parts/directory-tile\'); ?>
                                <?php if(get_field(\'deals\' ) != 0) $deal_count++; ?>

                            <?php
                            // ---------------------------------------------------------------------------------
                            // End loop for main archives
                            // ---------------------------------------------------------------------------------
                            endwhile; ?>


                            <div class="seperator"></div>
                        </div><?php  // closes flexible row ?>

                        <div class="seperator"></div>
                    </div> <?php // closes primary ?>
                </div>

                <div class="span4 pull-left">
                    <?php get_template_part(\'parts/board\'); ?>
                </div>

            </div><?php  // closes flexible row ?>

            <?php wp_pagenavi(); ?>

            <div class="seperator"></div>
        </div> <?php // closes container ?>

<?php 
else : ?>

<div class="bg-map">
        <?php include @ (\'includes/include-archive-map.php\'); ?>
    </div>

    <div class="container header-container">


        <!--    archive header -->
        <header class="page-header entry-header">

            <div class="header-inner">

                <h1 class="page-title">

                    <img src="<?php bloginfo( \'stylesheet_directory\' ); ?>/assets/css/images/the.png" alt="THE" class="the" />

                    SEARCH RESULTS
                </h1>
                <h2>No Results</h2>


            <div class="seperator"></div> 
            </div>

        </header>
    <div class="seperator"></div> 
    </div>

    <div class="content-bg">

        <div class="container">                

            <div class="primary" id="primary" role="main">       

                <div class="flexible-row featured" id="listings">


                    <article id="post-0" class="post no-results not-found padding-trbl">

                        <h2><?php _e( \'Sorry, nothing was found\', \'parentheme\' ); ?></h2>


                        <div class="entry-content">
                            <p>No results were found for the search: <strong><?php

                            if ( $a ) :
                                $a = str_replace("+", " ", $a);
                                echo $a .\' \'; endif;
                            if ( $t ) :
                                $t = str_replace("-", " ", $t);
                                echo $t .\' \'; endif;
                            if ( $p ) :
                                echo $p; endif;
                        ?></strong>. Maybe broadening your search will help?</p>

                        </div><!-- .entry-content -->

                    </article><!-- #post-0 -->

                    <div class="seperator"></div>
                </div><?php  // closes flexible row ?>



                <div class="seperator"></div>
            </div> <?php // closes primary ?>

            <?php //get_sidebar(); ?>


            <?php wp_pagenavi(); ?>

            <div class="seperator"></div>
        </div> <?php // closes container ?>

    <?php endif; ?>

    </div><?php // closes content-bg ?>

</section><?php // ends #content ?>    
<?php get_footer(); ?>
自定义查询。php

<?php

function get_directory_search_query($s=\'\', $t=\'\', $a=\'\', $p=\'\') {

    if(empty($s)) $s = $_GET[\'s\'];
    if(empty($a)) $a = $_GET[\'a\'];
    if(empty($t)) $t = $_GET[\'t\'];
    if(empty($p)) $p = $_GET[\'p\'];

    if ( $a  ) :
        $search_args = array(
            \'meta_query\' => array(
                array(
                    \'key\' => \'area-2\',
                    \'value\' => $a,
                    \'compare\' => \'LIKE\'
                )
            )
        );
    endif;


    if ( $t  ) :
        $search_args = array(
            \'tax_query\' => array(
                array(
                    \'taxonomy\' => \'types\',
                    \'field\' => \'slug\',
                    \'terms\' => $t
                )
            )
        );
    endif;


    if ( $p  ) :
        $search_args = array(
            \'meta_query\' => array(
                array(
                    \'key\' => \'price\',
                    \'value\' => $p,
                    \'compare\' => \'=\'
                )
            )
        );
    endif;


    if ( $a  && $t ) :
        $search_args = array(
            \'meta_query\' => array(
                array(
                    \'key\' => \'area-2\',
                    \'value\' => $a,
                    \'compare\' => \'LIKE\'
                )
            ),
            \'tax_query\' => array(
                array(
                    \'taxonomy\' => \'types\',
                    \'field\' => \'slug\',
                    \'terms\' => $t
                )
            )
        );
    endif;

    if ( $a  && $p ) :
        $search_args = array(
            \'meta_query\' => array(
                \'relation\' => \'AND\',
                array(
                    \'key\' => \'area-2\',
                    \'value\' => $a,
                    \'compare\' => \'LIKE\'
                ),
                array(
                    \'key\' => \'price\',
                    \'value\' => $p,
                    \'compare\' => \'=\'
                )
            )

        );
    endif;

    if ( $p  && $t ) :
        $search_args = array(
            \'meta_query\' => array(
                array(
                    \'key\' => \'price\',
                    \'value\' => $p,
                    \'compare\' => \'=\'
                )
            ),
            \'tax_query\' => array(
                array(
                    \'taxonomy\' => \'types\',
                    \'field\' => \'slug\',
                    \'terms\' => $t
                )
            )
        );
    endif;

    if ( $a  && $p && $t ) :
        $search_args = array(
            \'meta_query\' => array(
                \'relation\' => \'AND\',
                array(
                    \'key\' => \'area-2\',
                    \'value\' => $a,
                    \'compare\' => \'LIKE\'
                ),
                array(
                    \'key\' => \'price\',
                    \'value\' => $p,
                    \'compare\' => \'=\'
                )
            ),
            \'tax_query\' => array(
                array(
                    \'taxonomy\' => \'types\',
                    \'field\' => \'slug\',
                    \'terms\' => $t
                )
            )
        );

    endif;

    $search_args[\'post_type\'] = \'directory\';
    $search_args[\'posts_per_page\'] = -1;
    $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
    $search_args[\'paged\'] = $paged;

    $search_query = new WP_Query( $search_args );

    return $search_query;
}
董事会。php

<?php

global $search_query;
global $deal_count;
$args = array_merge(
    $search_query->query_vars,
    array(
        \'posts_per_page\' => 3,
        \'orderby\' => \'rand\'
    )
);

$args[\'meta_query\'][] = array(
    \'key\'     => \'deals\',
    \'value\'   => \'0\',
    \'compare\' => \'!=\'
);

$args[\'post_type\'] = \'directory\';

$board_query = new WP_Query( $args ); ?>

<?php if( $board_query->have_posts()): ?>
    <div class="board">

        <h1 class="board__title">The Board</h1>

            <div class="board__content">

                <?php while($board_query->have_posts()): $board_query->the_post(); ?>
                    <?php get_template_part(\'parts/offer\'); ?>
                <?php endwhile; ?>

            </div>

        <?php if($deal_count > 3) { ?>
            <?php
            $get_data = \'\';
            $data_vars = array(\'s\', \'a\', \'t\', \'p\');
            foreach($data_vars as $data_var){
                if(!empty($_GET[$data_var])) $get_data .= \'data-\'.$data_var.\'="\'.$_GET[$data_var].\'" \';
            }
            ?>
            <button class="board__refresh" <?php echo $get_data; ?>>Refresh Board</button>
        <?php } ?>

    </div>
    <?php wp_reset_postdata(); ?>
<?php endif; ?>
这是$args get传递给的数组new WP_Query($args):

Array
(
[meta_query] => Array
(
[0] => Array
(
[key] => area-2
[value] => Bank
[compare] => LIKE
)

[1] => Array
(
[key] => deals
[value] => 0
[compare] => !=
)

)

[posts_per_page] => 3
[paged] => 1
[post_type] => directory
[error] =>
[m] =>
[p] => 0
[post_parent] =>
[subpost] =>
[subpost_id] =>
[attachment] =>
[attachment_id] => 0
[name] =>
[static] =>
[pagename] =>
[page_id] => 0
[second] =>
[minute] =>
[hour] =>
[day] => 0
[monthnum] => 0
[year] => 0
[w] => 0
[category_name] =>
[tag] =>
[cat] =>
[tag_id] =>
[author] =>
[author_name] =>
[feed] =>
[tb] =>
[comments_popup] =>
[meta_key] =>
[meta_value] =>
[preview] =>
[s] =>
[sentence] =>
[fields] =>
[menu_order] =>
[category__in] => Array
(
)

[category__not_in] => Array
(
)

[category__and] => Array
(
)

[post__in] => Array
(
)

[post__not_in] => Array
(
)

[tag__in] => Array
(
)

[tag__not_in] => Array
(
)

[tag__and] => Array
(
)

[tag_slug__in] => Array
(
)

[tag_slug__and] => Array
(
)

[post_parent__in] => Array
(
)

[post_parent__not_in] => Array
(
)

[author__in] => Array
(
)

[author__not_in] => Array
(
)

[ignore_sticky_posts] =>
[suppress_filters] =>
[cache_results] => 1
[update_post_term_cache] => 1
[update_post_meta_cache] => 1
[nopaging] => 1
[comments_per_page] => 50
[no_found_rows] =>
[order] => DESC
[orderby] => rand
)
我已经尝试了我能想到的一切。我试过设置\'posts_per_page\' 尽可能使用参数。我试过使用\'showposts\', \'posts_per_archive_page\', 和\'nopaging\'.

我只是不明白一个新的查询,在正确设置参数的情况下,是如何表现为这样的。

谢谢你的帮助。

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

在你的垃圾堆里$args 在上面nopaging 设置为1 这将禁用分页(相当于posts_per_page = -1).

尝试将此设置为false 看看它是否能解决您的问题。然后,您可以查看设置的原因。

结束

相关推荐

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

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