WordPress.org

Ready to get started?Download WordPress

Forums

WTI Like Post
More than a number of Likes (6 posts)

  1. diegonavland
    Member
    Posted 9 months ago #

    Hi I was wondering if anybody knows how can I show post in a page that only have more than a certain number of likes, I would really appreciate any help.

    http://wordpress.org/plugins/wti-like-post/

  2. webtechideas
    Member
    Plugin Author

    Posted 9 months ago #

    Please use the following piece of query to get the result. Change the $min_value as per your requirement.

    // Getting the most liked posts
    $min_value = 5;

    $query = "SELECT post_id, SUM(value) AS like_count, post_title, post_author FROM {$wpdb->prefix}wti_like_post L, {$wpdb->prefix}posts P
    WHERE L.post_id = P.ID AND post_status = 'publish' GROUP BY post_id HAVING SUM(value) > $min_value";

    Thanks

  3. diegonavland
    Member
    Posted 9 months ago #

    Thanks for the reply, but I'm getting an error:

    Fatal error: Call to a member function is_singular() on a non-object

    I placed the code with pre_get_posts like this:

    function hotposts($query){
    if( is_home() && $query->is_main_query() ){
    $min_value = 5;

    $query = "SELECT post_id, SUM(value) AS like_count, post_title, post_author FROM {$wpdb->prefix}wti_like_post L, {$wpdb->prefix}posts P
    WHERE L.post_id = P.ID AND post_status = 'publish' GROUP BY post_id HAVING SUM(value) > $min_value";
    }
    }
    add_action('pre_get_posts', 'hotposts');

  4. webtechideas
    Member
    Plugin Author

    Posted 9 months ago #

    I am not sure about the exact issue but seems like the $query variable in the 1st line does not exist. Since you have put the code inside a function, you need to put the following code as the 1st line inside the function.

    global $wpdb;

    Then you can get the result using the following code after the query.

    $result = $wpdb->get_results($query);

    You can refer to WordPress database functions for more reference.

    Note:
    - I would suggest to have a different variable for the custom query.

    Thanks

  5. diegonavland
    Member
    Posted 9 months ago #

    Thanks but I'm still getting the same error, the new code is something like this:

    function hotposts($query){

    global $wpdb;

    if( is_home() && $query->is_main_query() ){
    $min_value = 5;

    $query = "SELECT post_id, SUM(value) AS like_count, post_title, post_author FROM {$wpdb->prefix}wti_like_post L, {$wpdb->prefix}posts P
    WHERE L.post_id = P.ID AND post_status = 'publish' GROUP BY post_id HAVING SUM(value) > $min_value";
    }

    $result = $wpdb->get_results($query);
    }
    add_action('pre_get_posts', 'hotposts');

    Any other ideas on what may be happening? Or is there another way to do this?

  6. webtechideas
    Member
    Plugin Author

    Posted 8 months ago #

    I am not sure about your set up also why you are attaching this functionality with pre_get_posts hook. Can you please contact me here http://www.webtechideas.com/contact-us/ ?

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.