我的职能如下。php:-
/**
* Shortcode: Latest Properties
*/
function latest_properties_func( $atts ) {
$args = array(
\'posts_per_page\'=> 5,
\'post_type\' => \'properties\',
\'orderby\' => \'post_date\',
\'order\' => \'DESC\',
);
$latest_query = new WP_Query( $args );
$latest_properties = \'\';
if( $latest_query->have_posts() ):
while( $latest_query->have_posts() ) : $latest_query->the_post();
$latest_price = get_field(\'house_price\');
$latest_properties .= \'<a href="\'.get_the_permalink().\'">\';
$latest_properties .= \'<div class="featured-wrapper">\';
$latest_properties .= \'<div class="col-md-5">\';
$latest_properties .= get_the_post_thumbnail() . \'<br />\';
$latest_properties .= \'</div>\';
$latest_properties .= get_the_title() . \'<br />\';
$latest_properties .= get_field(\'house_type\') . \'<br />\';
$latest_properties .= \'£\' . number_format($latest_price) . \'<br />\';
$latest_properties .= \'</div>\';
$latest_properties .= \'</a>\';
$latest_properties .= \'<div class="clearfix"></div>\';
endwhile;
endif; wp_reset_query();
return $latest_properties;
}
add_shortcode( \'latest_properties\', \'latest_properties_func\' );
这显示了最新的属性
http://website.dev/properties/ 然而,当我访问时
http://website.dev/properties/?house_town=Leicester 它只显示house\\u town=Leicester的最新房产。
我意识到是我的搜索功能导致了这种情况,如下所示:-
// array of filters (field key => field name)
$GLOBALS[\'my_query_filters\'] = array(
\'field_1\' => \'house_status\',
\'field_2\' => \'house_type\',
\'field_3\' => \'house_town\',
\'field_4\' => \'house_price\'
);
// action
add_action(\'pre_get_posts\', \'my_pre_get_posts\', 10, 1);
function my_pre_get_posts( $query ) {
// bail early if is in admin
if( is_admin() ) {
return;
}
// get meta query
$meta_query = $query->get(\'meta_query\');
// loop over filters
foreach( $GLOBALS[\'my_query_filters\'] as $key => $name ) {
// continue if not found in url
if( empty($_GET[ $name ]) ) {
continue;
}
// get the value for this filter
$value = explode(\',\', $_GET[ $name ]);
// append meta query
$meta_query[] = array(
\'key\' => $name,
\'value\' => $value,
\'compare\' => \'IN\',
);
}
// update meta query
$query->set(\'meta_query\', $meta_query);
}
我的问题是,我如何将这两者结合起来,使它们彼此协同工作?