Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author webtechideas

    (@webtechideas)

    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

    Thread Starter diegonavland

    (@diegonavland)

    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’);

    Plugin Author webtechideas

    (@webtechideas)

    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

    Thread Starter diegonavland

    (@diegonavland)

    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?

    Plugin Author webtechideas

    (@webtechideas)

    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 https://www.webtechideas.com/contact-us/ ?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘More than a number of Likes’ is closed to new replies.