因此,我正在尝试在自定义帖子类型存档页面上使用Advanced Custom Filters Pro插件创建一个过滤器,但未过滤。尽管无论选择什么,它都会列出页面上的所有项目,但它会更改URL。
我正在尝试按照以下教程进行复制:https://www.advancedcustomfields.com/resources/creating-wp-archive-custom-field-filter/
我用以下命令设置了functions.php:
<?php
function theme_enqueue_styles() {
wp_enqueue_style( 'aerious-parent-stylesheet', get_template_directory_uri() . '/style.css', array( 'aerious-google-fonts', 'aerious-libs' ) );
}
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
function aerious_child_lang_setup() {
$lang = get_stylesheet_directory() . '/languages';
load_child_theme_textdomain( 'aerious', $lang );
}
add_action( 'after_setup_theme', 'aerious_child_lang_setup' );
// Houses Custom Posttype
$labels = array(
'name' => 'Houses',
'singular_name' => 'House',
'add_new' => 'Add New',
'add_new_item' => 'Add New House',
'edit_item' => 'Edit Houses',
'new_item' => 'New House',
'all_items' => 'All Houses',
'view_item' => 'View House',
'search_items' => 'Search Houses',
'not_found' => 'No houses found',
'not_found_in_trash' => 'No houses found in Trash',
'parent_item_colon' => '',
'menu_name' => 'Houses',
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'houses' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'supports' => array('title', 'editor', 'author', 'thumbnal')
);
register_post_type( 'house', $args);
// Custom Posttype Filter
// array of filters (field key => field name)
$GLOBALS['my_query_filters'] = array(
'field_5d1cafdb2f7d4' => 'bedrooms'
);
// 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;
// bail early if not main query
// - allows custom code / plugins to continue working
if( !$query->is_main_query() ) 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
// eg: http://www.website.com/events?city=melbourne,sydney
$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);
// echo '<pre>';
// var_dump($query->query_vars);
// echo '</pre>';
}
在我的archive-house.php中:
<?php
global $aerious_options;
get_header();
do_action( 'aerious_main_layout_start' );
$class_blog_layout = isset( $aerious_options['blog_layout'] ) ? esc_html( $aerious_options['blog_layout'] ) : 'standard';
$classes = array( 'aerious-blog-posts', 'blog-layout-'. $class_blog_layout );
$blog_pagination_type = isset( $aerious_options['blog_pagination_type'] ) ? esc_attr( $aerious_options['blog_pagination_type'] ) : 'pagination';
$blog_archive_layout = isset( $aerious_options['blog_archive_layout'] ) ? esc_attr( $aerious_options['blog_archive_layout'] ) : 'standard';
if ( $blog_pagination_type == 'loadmore' ) {
$classes[] = 'pagination-container';
}
else {
$classes[] = 'row';
}
if( $class_blog_layout == 'grid' ) {
$classes[] = 'posts-grid';
}
if ( have_posts() ){ ?>
<div <?php echo aerious_class( $classes ) ?> >
<!-- FILTER START -->
<div id="archive-filters">
<?php foreach( $GLOBALS['my_query_filters'] as $key => $name ):
// get the field's settings without attempting to load a value
$field = get_field_object($key, false, false);
// set value if available
if( isset($_GET[ $name ]) ) {
$field['value'] = explode(',', $_GET[ $name ]);
}
// create filter
?>
<div class="filter" data-filter="<?php echo $name; ?>">
<?php create_field( $field ); ?>
</div>
<?php endforeach; ?>
</div>
<script type="text/javascript">
(function($) {
// change
$('#archive-filters').on('change', 'input[type="radio"]', function(){
// vars
var url = '<?php echo home_url('houses'); ?>';
args = {};
// loop over filters
$('#archive-filters .filter').each(function(){
// vars
var filter = $(this).data('filter'),
vals = [];
// find checked inputs
$(this).find('input:checked').each(function(){
vals.push( $(this).val() );
});
// append to args
args[ filter ] = vals.join(',');
});
// update url
url += '?';
// loop over args
$.each(args, function( name, value ){
url += name + '=' + value + '&';
});
// remove last &
url = url.slice(0, -1);
// reload page
window.location.replace( url );
});
})(jQuery);
</script>
<!-- FILTER END -->
<?php
if ( $blog_pagination_type == 'loadmore' OR $blog_archive_layout == 'grid' ) : ?>
<div class="<?php if ( $blog_pagination_type == 'loadmore' ) { echo 'posts-append grid row'; } else { echo 'grid'; } ?>"> <?php
endif;
while ( have_posts() ) { the_post();
get_template_part( 'template-parts/blog/content', 'loop' );
}
if ( $blog_pagination_type == 'loadmore' OR $blog_archive_layout == 'grid' ) : ?>
</div><?php
endif;
if ( $blog_pagination_type == 'loadmore' ) {
$big = 999999999;
$loadmore_text = ( isset( $aerious_options['loadmore_text'] ) && $aerious_options['loadmore_text'] ) ? esc_html( $aerious_options['loadmore_text'] ) : esc_html__( 'Load More', 'aerious' );
echo '<div class="pagination-loadmore">';
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var( 'paged' ) ),
'total' => $wp_query->max_num_pages,
'next_text' => '<span class="aerious-btn aerious-btn-default aerious-btn-lg">'. $loadmore_text .'<span class="aerious-count-loader"><img src="'. get_template_directory_uri(). "/assets/images/loaders/loader.gif" .'" alt="..."></span></span>',
) );
echo '</div>';
}
else {
aerious_pagination();
}
?>
</div> <?php
}
else {
get_template_part( 'template-parts/content','none' );
}
do_action( 'aerious_main_layout_end' );
get_footer();
搜索地址为:
https://startnow.hu/houses/?bedrooms=1,3
调试与:
echo "<pre>"; print_r($wp_query->query_vars); echo "</pre>";
结果:
array(54) {
["post_type"]=>
string(5) "house"
["error"]=>
string(0) ""
["m"]=>
string(0) ""
["p"]=>
int(0)
["post_parent"]=>
string(0) ""
["subpost"]=>
string(0) ""
["subpost_id"]=>
string(0) ""
["attachment"]=>
string(0) ""
["attachment_id"]=>
int(0)
["name"]=>
string(0) ""
["static"]=>
string(0) ""
["pagename"]=>
string(0) ""
["page_id"]=>
int(0)
["second"]=>
string(0) ""
["minute"]=>
string(0) ""
["hour"]=>
string(0) ""
["day"]=>
int(0)
["monthnum"]=>
int(0)
["year"]=>
int(0)
["w"]=>
int(0)
["category_name"]=>
string(0) ""
["tag"]=>
string(0) ""
["cat"]=>
string(0) ""
["tag_id"]=>
string(0) ""
["author"]=>
string(0) ""
["author_name"]=>
string(0) ""
["feed"]=>
string(0) ""
["tb"]=>
string(0) ""
["paged"]=>
int(0)
["meta_key"]=>
string(0) ""
["meta_value"]=>
string(0) ""
["preview"]=>
string(0) ""
["s"]=>
string(0) ""
["sentence"]=>
string(0) ""
["title"]=>
string(0) ""
["fields"]=>
string(0) ""
["menu_order"]=>
string(0) ""
["embed"]=>
string(0) ""
["category__in"]=>
array(0) {
}
["category__not_in"]=>
array(0) {
}
["category__and"]=>
array(0) {
}
["post__in"]=>
array(0) {
}
["post__not_in"]=>
array(0) {
}
["post_name__in"]=>
array(0) {
}
["tag__in"]=>
array(0) {
}
["tag__not_in"]=>
array(0) {
}
["tag__and"]=>
array(0) {
}
["tag_slug__in"]=>
array(0) {
}
["tag_slug__and"]=>
array(0) {
}
["post_parent__in"]=>
array(0) {
}
["post_parent__not_in"]=>
array(0) {
}
["author__in"]=>
array(0) {
}
["author__not_in"]=>
array(0) {
}
["meta_query"]=>
array(3) {
["key"]=>
string(8) "bedrooms"
["value"]=>
array(1) {
[0]=>
string(1) "1"
}
["compare"]=>
string(2) "IN"
}
}
NULL
答案 0 :(得分:0)
好的,我已经解决了问题!
在functions.php中,教程说要写
// append meta query
$meta_query[] = array(
'key' => $name,
'value' => $value,
'compare' => 'IN',
);
但是我们需要
// append meta query
$meta_query = [];
$meta_query[] = array(
'key' => $name,
'value' => $value,
'compare' => 'IN',
);
仍然感谢您的帮助!