iamhrc on "Pagination Not Retaining Query"

ساخت وبلاگ

Hello,

I've written this script and can't seem to get it to work right.

Background: I have a custom search for a real estate website and need to search for posts by number of rooms, neighborhood, amenities and price range. The script works but when trying to paginate it looses it's query information. It's almost like it's not retaining the query and starting a new one. I suspect this because of my debug code...so my code is running right at first but then when I hit next it shows the results as if I didn't select anything.

I know I'm over looking something so any help is greatly appreciated!

<?php
/** * Template Name: Listings
**/ // Get Neighborhood info $neighborhood = $_POST['neighborhood']; $totalneighborhood = count($neighborhood); $n=0; // Gather selected listings, if nothing is selected display all. $neighborhoodlist=array(); if (!empty($_POST['neighborhood'])) { foreach($neighborhood as $n_value) { $neighborhoodlist[]=$n_value; } } else { $neighborhoodlist=array('Bloomfield', 'Downtown', 'East Liberty', 'Lawrenceville', 'Oakland', 'North Side', 'Regent Square', 'Shadyside', 'South Side', 'Strip District', 'Squirrel Hill', 'Uptown'); } // Get Bedrooms info $bedrooms = $_POST['bedrooms']; $totalbedrooms = count($bedrooms); $x=0; // Gather selected bedrooms, if nothing is selected display all. $bedroomnum=array(); if (!empty($_POST['bedrooms'])) { foreach($bedrooms as $x => $x_value) { $bedroomnum[]=$x_value; } } else { $bedroomnum=array('Studio', '1', '2', '3+'); } // Get Amenities info $amenities = $_POST['amenities']; $totalamenities = count($amenities); $a=0; // Gather selected amenities, if nothing is selected display all. $amenitiesnum=array(); if (!empty($_POST['amenities'])) { foreach($amenities as $a => $a_value) { // Query for amenities array for selected values that are "LIKE" in the array. $amenitiesnum[]=array('key'=>'amenities','value'=>$a_value,'compare'=>'LIKE'); } } else { $amenitiesnum=array('Integrated Parking', 'Pet Friendly', 'Gym', 'In-Unit Laundry', 'Rooftop/Private Outdoor Space', 'Pool', 'ZipCar Onsite'); } // Display Price // $minPrice Logic if (empty($_POST['minPrice'])) { $minPrice = 1; } else { $minPrice = $_POST['minPrice']; } // $maxPrice Logic if (empty($_POST['maxPrice'])) { $maxPrice = 9999; } else { $maxPrice = $_POST['maxPrice']; }
?>
<?php Starkers_Utilities::get_template_parts( array( '/shared/html-header', '/shared/header' ) ); ?>
<article>
<section> <h2><?php the_title(); ?></h2> <div class="searchlistings"> <h3>Search listings</h3> <?php Starkers_Utilities::get_template_parts( array( '/shared/searchlistings' ) ); ?></div>
<ul class="listings"> <?php // Initiates pagination. if ( get_query_var('paged') ) { $paged = get_query_var('paged'); } elseif ( get_query_var('page') ) { $paged = get_query_var('page'); } else { $paged = 1; } // An array querying results from an array_merge of arrays to list all selected values from the neighborhood, bedroom & amenities arrays. $args_listings = array('cat'=>'listings','posts_per_page' => 3, 'paged' => $paged,'meta_query'=> array_merge( array('relation'=>'AND', array('key'=>'neighborhood','value'=>$neighborhoodlist,'compare'=>'IN'), array('key'=>'bedrooms','value'=>$bedroomnum,'compare'=>'IN'), array('key'=>'price','value'=>array($minPrice,$maxPrice),'compare'=>'BETWEEN','type'=>'numeric'), ), $amenitiesnum), ); // Run Query visible by WordPress query_posts($args_listings); // Start the listings loop for all selected items in the array. $listings = new WP_Query( $args_listings ); if ($listings->have_posts()) : while ($listings->have_posts() ) : $listings->the_post(); ?><li> <a href="<?php esc_url( the_permalink() ); ?>" title="Permalink to <?php the_title(); ?>" rel="bookmark"> <?php // check if the post has a Post Thumbnail assigned to it. $postTitle = the_title( '', '', false); if ( has_post_thumbnail() ) : the_post_thumbnail('thumbnail', array('class' => 'thumbnail', 'alt' => $postTitle, 'title' => $postTitle )); endif;?> </a> <h3><a href="<?php esc_url( the_permalink() ); ?>" title="Permalink to <?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a></h3> <h4><em><?php the_field('neighborhood'); ?></em></h4> <h4><em>Bedrooms:</em> <?php the_field('bedrooms'); ?></h4> <h4><em>Price:</em> $<?php $output = get_field('price'); $price = number_format( $output, 0,',', ','); echo $price; ?></h4> </li><?php endwhile; ?>
<div class="postlists"><p><?php posts_nav_link(' ','previous page','next page'); ?></p></div> <?php else: // Message shown if nothing matches their query. echo "<h1>There are currently no listings that meet your search criteria.</h1>"; endif; ?> <?php wp_reset_postdata(); ?>
</ul> <div class="clear"></div>
<div class="searchlistings"> <h3>Try a different search</h3> <?php Starkers_Utilities::get_template_parts( array( '/shared/searchlistings' ) ); ?> </div>
<?php // Debug Code
echo '<hr />';
echo '<p><b><u>Test Queries</u></b></p>';
echo '<p>' . '$neighborhoodlist: ' . json_encode($neighborhoodlist) . '</p>';
echo '<p>' . '$bedroomnum: ' . json_encode($bedroomnum) . '</p>';
echo '<p>' . '$amenitiesnum: ' . json_encode($amenitiesnum) . '</p>';
echo '<p>' . '$price: ' . $price . '</p>';
echo '<p>' . '$args_listings: ' . json_encode($args_listings) . '</p>';
echo '<p>' . '$minPrice: ' . $minPrice . '</p>';
echo '<p>' . '$maxPrice: ' . $maxPrice . '</p>';
if ( $maxPrice > 1 ) { echo 'max is greater than 1<br>';} else { echo 'max is less than 1<br>'; }
if ( $maxPrice > 10000 ) { echo 'max is greater than 10000<br>';} else { echo 'max is less than 10000<br>'; }
if ( $maxPrice > 4000 ) { echo 'max is greater than 4000<br>';} else { echo 'max is less than 4000<br>'; }
if ( $minPrice > 1 ) { echo 'min is greater than 1<br>';} else { echo 'min is less than 1<br>'; }
if ( $minPrice > 10000 ) { echo 'min is greater than 10000<br>';} else { echo 'min is less than 10000<br>'; }
if ( $minPrice > 4000 ) { echo 'min is greater than 4000<br>';} else { echo 'min is less than 4000<br>'; }
echo '<hr />';
?>
</section>
<aside> <?php dynamic_sidebar('ad-widget'); ?> <?php the_field('sidebar_info'); ?> <?php dynamic_sidebar('sidebar-widget'); ?> </aside> <div class="clear"></div>
</article>
<?php Starkers_Utilities::get_template_parts( array( '/shared/footer','/shared/html-footer' ) ); ?>

WordPress ...
ما را در سایت WordPress دنبال می کنید

برچسب : نویسنده : استخدام کار wpss بازدید : 91 تاريخ : جمعه 10 ارديبهشت 1395 ساعت: 3:29