我有一个自定义帖子类型“plot-location”,我动态设置$ args数组和表单中的数据。提交时,表单在“第1页”上显示正确的结果但是当使用分页链接时,将返回所有自定义帖子。
设置帖子类型:
add_action('init', 'plot_register');
function plot_register() {
$args = array(
'label' => __('Plots'),
'singular_label' => __('Plot'),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => true,
'query_var' => true,
'has_archive' => true,
'rewrite' => array( 'slug' => 'house-type','with_front' => true ),
'_builtin' => false,
'supports' => array('title', 'editor', 'thumbnail', 'custom-fields', 'excerpt')
);
register_post_type( 'plot-location' , $args );
//create cats
register_taxonomy("plot-development", array("plot-location"), array("hierarchical" => true, "label" => "Plot Locations", "singular_label" => "Plot Location", "rewrite" => true));
}
The Loop:
function property_search(){
//declare vars
$area = "";
$price = "";
$type = "";
$beds = "";
//set the vars
if(isset($_POST['area']) && !empty($_POST['area']))
{
$area = array(
'taxonomy' => 'plot-development',
'field' => 'slug',
'terms' => strtolower ($_POST['area'])
);
}
//print_r($area);
if(isset($_POST['price']))
{
$price = array('key' => 'plotPrice-meta', 'value' => $_POST['price'], 'type' => 'NUMERIC', 'compare' => '>=');
}
else{
$price = array();
}
if(isset($_POST['type']) && !empty($_POST['type']))
{
$type = array('key' => 'plotType-meta','value' => $_POST['type'], 'compare' => '=') ;
}
else{
$type = array('key' => 'plotType-meta','value' => array('Detached', 'Semi', 'Mews', 'Terraced' ), 'compare' => 'IN');
}
if(isset($_POST['beds']))
{
$beds = array('key' => 'plotBeds-meta','value' => (int)$_POST['beds'],'compare' => '>=');
}
else{
$beds = array();
}
//echo $area.' '.$price.' '.$type.' '.$beds;
global $post;
global $page;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'plot-location',
'tax_query' => array(
$area
),
'posts_per_page' => 5,
'paged' => $paged,
'meta_query' => array(
'relation' => 'AND',
$price,
$type,
$beds
),
'orderby' => 'meta_value_num',
'meta_key' => 'plotPrice-meta',
'post_status' => 'publish',
'order' => 'ASC'
);
// the query
global $wp_query;
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
//the loop -->
while ( $the_query->have_posts() ) : $the_query->the_post();
$out .= '<hr />';
$out .= '<div class="plot-search-thumb">';
$out .= get_the_post_thumbnail($post->ID,'thumbnail');
$out .= '</div>';
$out .= '<div class="plot-search-cont">';
$out .= '<h2 class="plot-title">'.get_the_title().'</h2>';
$cus_terms = get_the_terms( $post->ID, 'plot-development' );
foreach($cus_terms as $term)
{
$ob_id = $term->object_id;
$term_slug = $term->slug;
$term_name = $term->name;
}
$out .= '<div style="font-weight: bold;">'.$term_name.'</div>';
//$out .= '<div class="plot-price"><strong>£'.(int)get_post_meta( $post->ID, 'plotPrice-meta', true).'</strong></div>' ;
$out .= '<div class="plot-price"><strong>£'.number_format ( get_post_meta( $post->ID, 'plotPrice-meta', true)).'</strong></div>' ;
$out .= '<div class="plot-excerpt">'.get_the_excerpt().'</div>';
$out .= '<div style="margin: 0 20px 0 0; float: left;"><a class="plot-search-link" href="'.get_the_permalink($post->ID).'">MORE INFO</a></div>';
$arg = array(
'post_type' => 'dev-location',
'meta_key' => 'dev-title',
'meta_value' => $term_slug,
'posts_per-page' => 1
);
$dev = get_posts($arg);
wp_reset_postdata();
$out .= '<div><a class="plot-search-link" href="/development/'.$dev[0]->post_name.'/">VIEW DEVELOPMENT</a></div>
</div>
<div style="clear: left;"></div>';
endwhile;
//end of the loop -->
//pagination here -->
$out .= '<div class="post-navigation">';
//ob_start();
if ( function_exists('wp_pagenavi')) {
$out .= wp_pagenavi( array(
'echo' => false,
'query' => $the_query
));
}
//$out .= ob_get_clean();
$out .='</div>';
else:
$out .= '<p>'. _e( 'Sorry, no plots matched your criteria.' ).'</p>';
endif;
wp_reset_postdata();
wp_reset_query();
return $out;
}
add_shortcode('property_search', 'property_search');
看起来“查询”var没有被传递,有人能指出我哪里错了吗?
答案 0 :(得分:0)
我最后通过将$ _POST值存储在$ _SESSION变量中来解决这个问题。
function property_search(){
//declare vars
$area = "";
$price = "";
$type = "";
$beds = "";
//set the vars
if(isset($_POST['form_submit']))
{
if(isset($_POST['area']) && !empty($_POST['area']))
{
$area = array(
'taxonomy' => 'plot-development',
'field' => 'slug',
'terms' => strtolower ($_POST['area'])
);
}
$_SESSION['ssd_area'] = $area;//worst case empty string
if(isset($_POST['price']))
{
$price = array('key' => 'plotPrice-meta', 'value' => $_POST['price'], 'type' => 'NUMERIC', 'compare' => '>=');
}
else{
$price = array();
}
$_SESSION['ssd_price'] = $price;
if(isset($_POST['type']) && !empty($_POST['type']))
{
$type = array('key' => 'plotType-meta','value' => $_POST['type'], 'compare' => '=') ;
}
else{
$type = array('key' => 'plotType-meta','value' => array('Detached', 'Semi', 'Mews', 'Terraced' ), 'compare' => 'IN');
}
$_SESSION['ssd_type'] = $type;
if(isset($_POST['beds']))
{
$beds = array('key' => 'plotBeds-meta','value' => (int)$_POST['beds'],'compare' => '>=');
}
else{
$beds = array();
}
$_SESSION['ssd_beds'] = $beds;
}
else{
//this needs error checks.
$area = $_SESSION['ssd_area'];
$price = $_SESSION['ssd_price'];
$type = $_SESSION['ssd_type'];
$beds = $_SESSION['ssd_beds'];
}
很想看到一些关于此的反馈:这是一个黑客,会在什么时候打破?等。
希望这有助于某人。