1

I've tried for hours for a solution to this. Here's the problem, I have a theme that is using new WP_Query to set up a loop. That works fine.

I want to take the original query, modify it, and then use another custom query to display the modified list of posts.

It all works fine except for the 'posts_per_page' parameter, which appears to be set correctly (see $args below), but shows all of the posts.

Here is my code:

directory-search.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(); ?>

custom-queries.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;
}

board.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; ?>

This is the $args array that get's passed to 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
)

What I've tried

Everything I can think of. I've tried setting the 'posts_per_page' parameter every way I could. I've tried using 'showposts', 'posts_per_archive_page', and 'nopaging'.

I just don't understand how a new query, with the correctly set parameters, behaves like this.

Thanks for your help.

2

In the dump of your $args above, nopaging is set to 1 which disables pagination (equivalent to posts_per_page = -1).

Try setting this to false and see if it fixes your issue. You can then look into why it's being set.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.