WordPress pre_get_posts 顶帖优先

发布于 2025-01-19 14:42:26 字数 775 浏览 3 评论 0原文

我需要以下方面的一些帮助。我有一个带有自定义字段“user_define_field”的 WordPress 自定义帖子类型。如果此字段值以“Top”开头,我希望它成为第一个帖子。到目前为止,我想出了这个:

    $query->set('meta_query', array(
        'relation' => 'OR',
        array(
            'key' => 'user_defined_field',
            'value' => '^Top',
            'compare' => 'REGEXP',
            'type' => 'STRING'
        ),
        array(
            'key' => 'verwaltung_techn-user_defined_simplefield-code',
            'compare' => 'NOT EXISTS',
            'type' => 'STRING'
        ),

    ));

不幸的是,这样它一直在加载,但从不显示结果。仅通过第一个元查询,它就显示了“Top”对象only: 大批( '键' => '用户定义字段', '值' => '^顶部', '比较' => '正则表达式', '类型' => '细绳' ),

另一个元查询也可以单独工作。但是当它们与关系=“OR”组合时,它不起作用。有什么想法吗?

I need a litte help with the following. I have a Wordpress custom post type with the custom field 'user_defined_field'. If this fields value starts with 'Top' I want it to be the first posting. So far I came up with this:

    $query->set('meta_query', array(
        'relation' => 'OR',
        array(
            'key' => 'user_defined_field',
            'value' => '^Top',
            'compare' => 'REGEXP',
            'type' => 'STRING'
        ),
        array(
            'key' => 'verwaltung_techn-user_defined_simplefield-code',
            'compare' => 'NOT EXISTS',
            'type' => 'STRING'
        ),

    ));

unfortunately like this it keeps loading and it never shows the result. With just the first meta query it shows me the "Top"-Object only:
array(
'key' => 'user_defined_field',
'value' => '^Top',
'compare' => 'REGEXP',
'type' => 'STRING'
),

the other meta query also works by itself. But when they're combined with relation="OR" it does not work. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯淡〆 2025-01-26 14:42:26

您可以尝试在这样的数组中包装或包装案例(未经测试):

    $query->set('meta_query', array(
    'relation' => 'OR',
array(
    array(
        'key' => 'user_defined_field',
        'value' => '^Top',
        'compare' => 'REGEXP',
        'type' => 'STRING'
    ),
    array(
        'key' => 'verwaltung_techn-user_defined_simplefield-code',
        'compare' => 'NOT EXISTS',
        'type' => 'STRING'
    ),
)
));

You could try wrapping both or cases in an array like this (untested):

    $query->set('meta_query', array(
    'relation' => 'OR',
array(
    array(
        'key' => 'user_defined_field',
        'value' => '^Top',
        'compare' => 'REGEXP',
        'type' => 'STRING'
    ),
    array(
        'key' => 'verwaltung_techn-user_defined_simplefield-code',
        'compare' => 'NOT EXISTS',
        'type' => 'STRING'
    ),
)
));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文