Make WordPress Core

Opened 9 years ago

Last modified 9 years ago

#42082 new enhancement

Support compare custom fields in WP_Meta_Query

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

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
9 years ago

  • Keywords needs-patch added
  • Version → 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.