Wordpress pre_get_posts Top-posting first

240 Views Asked by At

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?

1

There are 1 best solutions below

1
On

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'
    ),
)
));