How to Query posts from Database set by plugin

Im using a Like plugin wich adds a like button to my site. It saves the number of “likes” and so on in the database. The plugin also allows me to display the most liked posts (DESC) in a page by using a shortcode – unfortunately this function only displays the posts WITHOUT a pagination in a simple list format. Here is the Shortcode function from the plugin to display the most liked post:

 /*--------------------------------------------*
 * Shortcode //dot_recommended_top_posts
 *--------------------------------------------*/

function dot_recommended_top_posts( $atts, $content = null )
{

    // define attributes and their defaults
    // get our variable from $atts
    $atts = shortcode_atts( array(
        'container' => 'li',
        'number' => '10',
        'post_type' => 'post',
        'year' => '',
        'monthnum' => '',
        'show_count' => '1',
    ), $atts );

    global $wpdb;

        // empty params array to hold params for prepared statement
        $params = array();

        // build query string
        $sql = "SELECT * FROM $wpdb->posts, $wpdb->postmeta WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id";

        // add year
        if( '' !== $atts['year'] ) {
            $sql .= ' AND YEAR(post_date) = %d';
            $params[] = $atts['year'];
        }

        // add monthnum
        if( '' !== $atts['monthnum'] ) {
            $sql .= ' AND MONTH(post_date) = %d';
            $params[] = $atts['monthnum'];
        }

        // add post WHERE
        $sql .= " AND post_status = 'publish' AND post_type = %s AND meta_key = '_recommended'";
        $params[] = $atts['post_type'];

        // add order by and limit
        $sql .= " ORDER BY {$wpdb->postmeta}.meta_value+0 DESC LIMIT %d";
        $params[] = $atts['number'];

        // prepare sql statement
        $query = $wpdb->prepare( $sql, $params );

        // execute query
        $posts = $wpdb->get_results( $query );

        $return = '';

    foreach ($posts as $item) {
        $post_title = stripslashes( $item->post_title );
        $permalink = get_permalink( $item->ID );
        $post_count = $item->meta_value;

        $return .= '<' . esc_html( $atts['container'] ) . '>';
        $return .= '<a href="' . esc_url( $permalink ) . '" title="' . esc_attr( $post_title ) .'" rel="nofollow">' . esc_html( $post_title ) . '</a> ';

        if ( $atts['show_count'] == '1') {
            $return .= '<span class="votes">' . esc_html( $post_count ) . '</span> ';
        }

        $return .= '</' . esc_html( $atts['container'] ) . '>';

    }
    return $return;

}   //dot_recommended_top_posts

My Question:

How can i query the post with pagination from the database by using a custom query in a custom page template? I already have a page template wich displays post from another function with pagination perfectley. My goal is to adapt this page template to display the most liked posts from the plugin in descending order. Here the template wich works fine with the ohter function – but i need your help to build the query to get the most liked posts set by the plugin:

    <?php
    /*
    Template Name: _default
    */
    ?>

    <?php get_header(); ?>

    <div class="container">
        <div class="row subpage-title text-center">
            <h1><?php _e('Most Liked Articles', 'test'); ?></h1>
        </div>
    </div>

    <div class="container-fluid">
        <?php
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

     // Query Function

        get_template_part('index', 'masonry');
        get_footer();
    ?>

Read more here: How to Query posts from Database set by plugin

Leave a Reply

Your email address will not be published. Required fields are marked *