I have created a Database VIEW
on wp_posts
which selects its all columns and exposes an extra calculated columns (average). So my View is identical to wp_posts
plus one column, which I want to use for sorting at later stage.
- Is there a way to bind
WP_Query
to that view? - In general, is it possible to bind
WP_Query
to a custom table, which has all the mandatory columns ofWP_Query
?
I am able to get records with $wpdb->get_results( _custom_query_here_ )
, but that doesn't let me use WP_Query
features.
P.S.: I thought WP_Query::parse()
is the answer to my curiosity, but apparently not.
EDIT
Here is the VIEW query:
CREATE VIEW `calculated_posts`
AS
SELECT *,
(SELECT Avg(CAST(`meta_value` as SIGNED))
FROM `wp_commentmeta`
WHERE meta_key = 'rating'
AND comment_id IN (SELECT comment_id
FROM wp_comments
WHERE comment_post_id = ID)) AS
rating_average
FROM wp_posts;
Edit 2:
From the solution provided by @birgire here: https://wordpress.org/support/topic/changing-table-name-and-make-wordpress-still-working, I used the following code:
global $wpdb;
$wpdb->posts = 'calculated_posts';
The WP_Query runs successfully, but I can't access the field rating_average
.
WP_Query
incl. arguments as this might be important.