WordPress.org

Make WordPress Core

Opened 2 years ago

Last modified 20 months ago

#42082 new enhancement

Support compare custom fields in WP_Meta_Query

Reported by: mariovalney Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.9.4
Component: Query Keywords: needs-patch
Focuses: Cc:
PR Number:

Description

The syntax of WP_Meta_Query currently is limited to values you already know.

It's not possible to compare between two meta_values:

SELECT posts WHERE meta_value_one > meta_value_two

I propose allow compare two meta_fields. For example, the pseudocode above would be represented as a meta_query argument that looks like this:

<?php
array(
    'key'       => 'meta_value_one',
    'value'     => 'meta_value_two',
    'compare'   => '>',
    'type'      => 'META_VALUE'
),

A workaround can be found here and here hahaha.

Feedback is welcome.

Change History (1)

#1 @mariovalney
20 months ago

  • Keywords needs-patch added
  • Version set to 4.9.4

Fixing the proposed example (the parametes should be the key, not the value):

<?php
array(
    'key'       => 'meta_key_one',
    'value'     => 'meta_key_two',
    'compare'   => '>',
    'type'      => 'META_VALUE'
),

And proposing another approach to keep consistency in 'type' parameter (because its values are SQL data types) maybe we can add a new parameter (which will be ignored if "value" is presented):

<?php
array(
    'key'       => 'meta_key_one',
    'value_of'  => 'meta_key_two',
    'compare'   => '>',
    'type'      => 'NUMERIC',
),
Note: See TracTickets for help on using tickets.