[wp-trac] [WordPress Trac] #56168: Add a way easily perform advanced post fields queries
WordPress Trac
noreply at wordpress.org
Thu Jul 7 13:12:12 UTC 2022
#56168: Add a way easily perform advanced post fields queries
-----------------------------+-----------------------------
Reporter: thelevicole | Owner: (none)
Type: feature request | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Query | Version:
Severity: trivial | Keywords:
Focuses: |
-----------------------------+-----------------------------
Currently if you want to perform advanced queries on the posts table you
have to add your custom SQL to the query with the `posts_where` filter.
For example, let's say we want to get the next page with a `menu_order`
value greater than the current page we would currently do something like:
{{{#!php
<?php
add_filter( 'posts_where', function( $where, $query ) {
global $wpdb;
$value = $query->query['menu_order_gt'] ?? null;
if ( is_numeric( $value ) ) {
$where .= " AND {$wpdb->posts}.menu_order > {$value} ";
}
return $where;
}, 10, 2 );
$query = new WP_Query( [
'post_type' => 'page',
'posts_per_page' => 1,
'menu_order_gt' => (int)get_post_field( 'menu_order' )
] );
}}}
I suggest adding a class like `WP_Meta_Query` but for post fields meaning
we could achieve the same result as above but in a much cleaner and
simpler way...
{{{#!php
<?php
$query = new WP_Query( [
'post_type' => 'page',
'posts_per_page' => 1,
'field_query' => [
[
'field' => 'menu_order',
'value' => (int)get_post_field( 'menu_order' ),
'compare' => '>',
'type' => 'NUMERIC'
]
]
] );
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/56168>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list