Opened 17 months ago
#56168 new feature request
Add a way easily perform advanced post fields queries
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | trivial | Version: | |
Component: | Query | Keywords: | |
Focuses: | Cc: |
Description
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 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 $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' ] ] ] );
Note: See
TracTickets for help on using
tickets.