按自定义字段过滤帖子 - url 更新正确但过滤不起作用

filtering posts by custom fields - url updates correctly but filtering doesn't work

我正在尝试通过使用 ACF pro 创建的几个自定义字段来过滤自定义 post 类型(图库)中的 post。现在,URL 更新正确,但无论选择哪个选项,存档库页面上都不会显示 post。 (如果我使用 LIKE 而不是 IN 作为比较运算符,则显示所有 post 并过滤掉 none)。

我提到了 this tutorial and code snippets and the correction shared in the single reply to this post,并且这个论坛上的许多 post 也提出了类似的问题。有一次我让这个适用于单个字段过滤器,但第二个字段不起作用。我不确定我当时做对了什么,或者我现在做错了什么。调试日志显示以下设置没有错误:

这是我正在测试的字段:

字段键:field_6174a76da315c
字段名称:type_of_art
字段类型:复选框
选择:
视觉:视觉
文学 : 文学
表演 : 表演
其他:其他

这是我的函数文件中当前的代码:

// array of filters (field key => field name)
        $GLOBALS['my_query_filters'] = array( 
             'field_6174a76da315c' => 'type_of_art'
             // 'field_618edb3358d2c' => 'filter_test',

    
        );


        // 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 = [];
                $meta_query[] = array(
                    'key'       => $name,
                    'value'     => $value,
                    'compare'   => 'IN',
                );
                
            } 
            
            
            // update meta query
            $query->set('meta_query', $meta_query);

        }

这是我的存档库文件中的代码:

<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="checkbox"]', function(){

            // vars
            var url = '<?php echo home_url('gallery'); ?>';
                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>

</div>

我尝试在之后添加这行代码来调试:

<?php echo "<pre>"; print_r($wp_query->query_vars); echo "</pre>"; ?>

这里是选择“视觉”过滤选项的结果:

Array
(
    [post_type] => gallery
    [lang] => en
    [error] => 
    [m] => 
    [p] => 0
    [post_parent] => 
    [subpost] => 
    [subpost_id] => 
    [attachment] => 
    [attachment_id] => 0
    [name] => 
    [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] => 
    [paged] => 0
    [meta_key] => 
    [meta_value] => 
    [preview] => 
    [s] => 
    [sentence] => 
    [title] => 
    [fields] => 
    [menu_order] => 
    [embed] => 
    [category__in] => Array
        (
        )

    [category__not_in] => Array
        (
        )

    [category__and] => Array
        (
        )

    [post__in] => Array
        (
        )

    [post__not_in] => Array
        (
        )

    [post_name__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
        (
        )

    [update_post_term_cache] => 1
    [meta_query] => Array
        (
            [0] => Array
                (
                    [key] => type_of_art
                    [value] => Array
                        (
                            [0] => visual
                        )

                    [compare] => IN
                )

        )

    [ignore_sticky_posts] => 
    [suppress_filters] => 
    [cache_results] => 1
    [lazy_load_term_meta] => 1
    [update_post_meta_cache] => 1
    [posts_per_page] => 10
    [nopaging] => 
    [comments_per_page] => 50
    [no_found_rows] => 
    [taxonomy] => language
    [term] => en
    [order] => DESC
)

我已经确认我的一些测试图库 post 被指定为 type_of_art 的可视化。 (同样的结果也会发生在其他选择上)。

感谢观看。

以防万一有人遇到类似问题 — 我最终使用了一个插件 (Search & Filter Pro),经过一些花招,它完成了我需要做的事情。