Skip to main content
Post Undeleted by KittMedia
deleted 36 characters in body
Source Link
KittMedia
  • 241
  • 1
  • 9

I had the problem by myself for numeric meta values and pointed out that it the order of the query is also important. For me the NOT EXISTS query has to be the first one.

Example:

$query->set( 'orderby', 'meta_value''meta_value_num' );
$query->set( 'meta_query', [
    'relation' => 'OR',
    [ 'key' => 'your_meta_name', 'compare' => 'NOT EXISTS' ],
    [
        'key' => 'your_meta_name',
        'orderby' => 'meta_value_num',
        'type' => 'NUMBER',
        'compare' => 'EXISTS',
    ],
] );

This way it orders every item, whichAlso important in order to get the right direction for numeric values is queried by the NOT EXISTS bygeneral meta_value’orderby’ and uses the custom order by its numeric values if a valueto be set to EXISTS’meta_value_num’.

  Otherwise you have strange results for numeric values, e. g.:

1, 2, 20, 21, 3, 4, 5 …

Instead of:

1, 2, 3, 4, 5 … 20, 21

I had the problem by myself for numeric meta values and pointed out that it the order of the query is also important. For me the NOT EXISTS query has to be the first one.

Example:

$query->set( 'orderby', 'meta_value' );
$query->set( 'meta_query', [
    'relation' => 'OR',
    [ 'key' => 'your_meta_name', 'compare' => 'NOT EXISTS' ],
    [
        'key' => 'your_meta_name',
        'orderby' => 'meta_value_num',
        'type' => 'NUMBER',
        'compare' => 'EXISTS',
    ],
] );

This way it orders every item, which is queried by the NOT EXISTS by meta_value and uses the custom order by its numeric values if a value EXISTS.

  Otherwise you have strange results for numeric values, e. g. 1, 2, 20, 21, 3, 4, 5 …

I had the problem by myself for numeric meta values and pointed out that it the order of the query is also important. For me the NOT EXISTS query has to be the first one.

Example:

$query->set( 'orderby', 'meta_value_num' );
$query->set( 'meta_query', [
    'relation' => 'OR',
    [ 'key' => 'your_meta_name', 'compare' => 'NOT EXISTS' ],
    [
        'key' => 'your_meta_name',
        'compare' => 'EXISTS',
    ],
] );

Also important in order to get the right direction for numeric values is the general ’orderby’ to be set to ’meta_value_num’. Otherwise you have strange results for numeric values, e. g.:

1, 2, 20, 21, 3, 4, 5 …

Instead of:

1, 2, 3, 4, 5 … 20, 21

Post Deleted by KittMedia
Source Link
KittMedia
  • 241
  • 1
  • 9

I had the problem by myself for numeric meta values and pointed out that it the order of the query is also important. For me the NOT EXISTS query has to be the first one.

Example:

$query->set( 'orderby', 'meta_value' );
$query->set( 'meta_query', [
    'relation' => 'OR',
    [ 'key' => 'your_meta_name', 'compare' => 'NOT EXISTS' ],
    [
        'key' => 'your_meta_name',
        'orderby' => 'meta_value_num',
        'type' => 'NUMBER',
        'compare' => 'EXISTS',
    ],
] );

This way it orders every item, which is queried by the NOT EXISTS by meta_value and uses the custom order by its numeric values if a value EXISTS.

Otherwise you have strange results for numeric values, e. g. 1, 2, 20, 21, 3, 4, 5 …