WP_Query to work with custom view

Use a custom view in the front-end:

You can try to modify the SELECT queries in the front-end with the following (alpha) plugin:

<?php
/**
 * Plugin Name: wpdb - a custom SELECT view for the wp_posts table on the front-end
 * Version:     alpha
 */

! is_admin() && add_filter( 'query', function( $query ) {
    global $wpdb;
    $view = 'calculated_posts'; // <-- Edit to your needs.
    if( 'select' === mb_substr( strtolower( $query ) , 0, 6 ) )
        if( false !== mb_stripos( $query, $wpdb->posts ) )
            $query = str_ireplace( $wpdb->posts, $view, $query );

    return $query;
}, PHP_INT_MAX );

by using the query filter of the wpdb class.

We don’t want to modify the INSERT, UPDATE and DELETE queries.

This will only affect queries made from the native wpdb class, but not for example direct MySQLi calls.

Notice that there’s of course the possibility that plugins and themes can bybass the native wpdb class when connecting to the database.

There might also be examples of more complex queries, e.g. combination of SELECT and INSERT. The above plugin could be modified to adjust for these cases.

Notice: Remember to take a backup before trying.

Accessing the extra field of the custom view:

The extra rating_average field should now be available in the WP_Post instance, like:

$post->rating_average

We can also create a custom template tag:

function get_the_rating_average()   
{
    $post = get_post();
    return ! empty( $post ) ? $post->rating_average : false;
}  

Here I’m just modifying the structure of the get_the_ID() function.

The corresponding display function is:

function the_rating_average()   
{
    echo get_the_rating_average();
}  

We can now easily access the extra field in the loop:

$q = new WP_Query( array( 'posts_per_page' => '5' ) );
while( $q->have_posts() ) : $q->the_post();
    the_title();
    the_rating_average();  #<-- displaying our extra field here
endwhile;

We also better use a name for our extra field that’s not already in use by the default fields.

Leave a Comment