I have created a Database
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_Queryto that view?
- In general, is it possible to bind
WP_Queryto a custom table, which has all the mandatory columns of
I am able to get records with
$wpdb->get_results( _custom_query_here_ ), but that doesn't let me use
P.S.: I thought
WP_Query::parse() is the answer to my curiosity, but apparently not.
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;
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