另一个在 pre_get_posts() 中的 meta_query 返回 0 个结果。

huangapple go评论54阅读模式
英文:

another meta_query in pre_get_posts() that returns 0 results

问题

多年来,我一直在使用Stack Overflow,我总是能找到答案(或者能拼凑在一起的答案的一半),但这次我真的搞不清楚了...

所以,我有一个ACF文章对象字段(inicia_areas),将一个或多个自定义文章类型(CPT)B的文章与CPT A的文章关联起来。

目标是能够在CPT A的存档中筛选出与之关联的CPT B文章。没有太复杂的要求 另一个在 pre_get_posts() 中的 meta_query 返回 0 个结果。

我正在使用pre_get_posts()挂钩,因为我能够以这种方式按类别筛选文章。

为了测试目的,我简化了pre_get_posts()函数,并在meta_query中硬编码了值。然后,我将三篇CPT A的文章与帖子的slug“central-de-compras”关联起来,以获得一些结果。

代码:

function wpsites_query($query)
{
    if ($query->is_main_query() && !is_admin()) {

        if (is_post_type_archive('iniciativas')) {

            // 这些变量从表单返回数组
            global $filter_areas, $filter_temas;

            // -> 类别筛选 - 正常工作
            if ($filter_temas && count($filter_temas) > 0) {
                $cat_ids = [];
                foreach ($filter_temas as $temas) {
                    array_push($cat_ids, get_term_by('slug', $temas, 'category')->term_id);
                }
                $query->set('category__in', $cat_ids);
            }

            // -> ACF筛选 - 返回没有帖子
            if ($filter_areas && count($filter_areas) > 0) {
                $query->set(
                    'meta_query',
                    array(
                        array(
                            'key'     => 'inicia_areas',
                            'value'   => 'central-de-compras',
                            'compare' => '='
                        )
                    )
                );
            }
            $query->set('posts_per_page', 12);
        }

        return $query;
    }
}
add_action('pre_get_posts', 'wpsites_query');

如果($filter_areas...)返回true,那么查询已经被设置了。
我尝试了值为“post-name”和“ID”,并使用“=”,“LIKE”,“==”,“EXIST”的比较。将值转换为数组并使用“IN”比较,但什么都没有。没有结果。

我没有发布查询输出,因为太长了,但如果有帮助的话,我会发布出来。

提前感谢!
Pedro

英文:

For years I've been using stack overflow and I could always find an answer (or half answers that I could stitch together), but this time I really can't figure this out...

So, I have an ACF Post Object field (inicia_areas) that associates one or more posts of Custom Post Type (CPT) B to CPT A posts.

The objective is to be able to filter posts in the archive of CPT A that have CPT B posts associated to them. Nothing too fancy 另一个在 pre_get_posts() 中的 meta_query 返回 0 个结果。

I'm using pre_get_posts() hook, since I was able to filter posts by category this way.

For tests purposes I simplified the pre_get_posts() function and hardcoded the value in the meta_query. I then associated three CPT A posts to the post with the slug "central-de-compras" to get some results.

The code:

function wpsites_query($query)
{
    if ($query->is_main_query() && !is_admin()) {

        if (is_post_type_archive('iniciativas')) {

            // these vars return arrays from form
            global $filter_areas, $filter_temas;

            // -> Categories Filter - WORKING
            if ($filter_temas && count($filter_temas) > 0) {
                $cat_ids = [];
                foreach ($filter_temas as $temas) {
                    array_push($cat_ids, get_term_by('slug', $temas, 'category')->term_id);
                }
                $query->set('category__in', $cat_ids);
            }

            // -> ACF Filter - RETURNING NO POSTS
            if ($filter_areas && count($filter_areas) > 0) {
                $query->set(
                    'meta_query',
                    array(
                        array(
                            'key'     => 'inicia_areas',
                            'value'   => 'central-de-compras',
                            'compare' => '='
                        )
                    )
                );
            }
            $query->set('posts_per_page', 12);
        }

        return $query;
    }
}
add_action('pre_get_posts', 'wpsites_query');

The if($filter_areas... is returning true, so the query is being set.
I tried the values with "post-name" and "ID" and compare with "=", "LIKE", "==", "EXIST". Converted the value to an array and use the "IN" compare, but nothing. Zero results.

I didn't post the query output because TLDR, but if it helps, I'll post it.

Thanks in advance!
Pedro

答案1

得分: 0

meta_query正在检查inicia_areas元数据是否等于central-de-compras,但元数据的值是整数数组,这就是为什么查询没有返回任何结果的原因。调整您的元数据或查询,使类型匹配或逻辑期望正确的类型。

英文:

The meta_query is checking inicia_areas meta is equal to central-de-compras, but the meta data's value is an array of integers, which is why the query does not return any results. Adjust your meta data or your query so that the types match or the logic expects the correct type.

huangapple
  • 本文由 发表于 2023年6月15日 19:12:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76481876.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定