This topic has 19 replies, 3 voices, and was last updated 7 years by Radu.

  • Author
  • #169864
     kffaner
    Participant

    i could not find a working answer to this, i even tried other plugins, but i want to be able to add an option to sort articles by LIKES.

    For example, when i use KLE POSTS element, i want to be able to specify in the query to sort the posts by LIKES so that the ones with the most likes appear first.

    Can you please help me make this happen? I would appreciate any suggestions/support with this.

     

    #169900
     Laura
    Moderator

    Hello, will assign the ticket to a higher support level who can help and advise you in your query.
    Thanks! ?

    Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solution

    Laura Solanes - Graphic Designer and Web Designer

    Please be patient as I try to answer each topic as fast as i can.

    If you like the theme or the support you've received please consider leaving us a review on Themeforest!

    Always happy to help you 🙂

    #169901
     kffaner
    Participant

    sounds good, thanks, i am a little familiar with JQ and PHP editing, if that helps in helping me with this issue hehe

    #170091
     Radu
    Moderator

    Hi,

    I’ve tried to make a function that filters that but I cannot make it works, it require more time and i will try again this tomorrow, here are some resources if you are familiar with php maybe you will work around

    https://wordpress.stackexchange.com/questions/169999/multiple-orderby-in-pre-get-posts-action

    Cheers
    R.

    Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solution
    #170096
     kffaner
    Participant

    hi,

    thanks, please let me know if you can make it work, if not ill have to try to figure it out but im betting you know more on php than i do.

    #170285
     Radu
    Moderator

    Hi,

    Just add this php code to wp-content/themes/kleo-child/functions.php

    COPY CODE
    add_filter( 'pre_get_posts', 'show_posts_by_kleo_likes' );
    function show_posts_by_kleo_likes( $query ) {
        $query->set( 'meta_key', '_item_likes' );
        $query->set( 'orderby', 'meta_value_num' );
        $query->set( 'order', 'DESC' );
        return $query;
    }

    That’s all

    Cheers
    R.

    Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solution
    #170286
     kffaner
    Participant

    weird, if i add this code it causes the front page to disappear and be removed Oo any ideas?

    #170287
     kffaner
    Participant

    it causes a bunch of pages to disappear including the default buddypress pages. no idea why, can we troubleshoot this?

    #170289
     Radu
    Moderator

    Try to enable parent theme then back the child theme

    Let me know

    Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solution
    #170292
     kffaner
    Participant

    tried, when i do that each time when switching to child theme (where the changes were made) it gives me 404 for the pages that are supposed to be there.

    #170296
     kffaner
    Participant

    i changed (on theme settings) words “Likes” to “prayers” but i don’t think that should have any impact on this, those are just labels…

    #170585
     kffaner
    Participant

    bump

    #170738
     Radu
    Moderator

    Hi,

    The edit likes text options not affecting that.

    Just replace the code with this one

    COPY CODE
    
    add_filter( 'pre_get_posts', 'show_posts_by_kleo_likes' );
    function show_posts_by_kleo_likes( $query ) {
        if(is_home()) {
            $query->set('meta_key', '_item_likes');
            $query->set('orderby', 'meta_value_num');
            $query->set('order', 'DESC');
            return $query;
        }
    }
    

    Will run only on blog/ page

    Cheers
    R

    Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solution
    #170740
     kffaner
    Participant

    nope, did not work, the option on the drop down menu is not there. I pasted the code into child themes function.php file and i switched back and forward from child theme to main theme and back to child theme. Still no option.

    Attachments:
    You must be logged in to view attached files.
    #171063
     Radu
    Moderator

    Hi,

    The snippet will not add the option there, it will sort the posts from the page (blog page) by likes.

    So, the previous code will work only on blog, page, if you want to sort the posts by Cleo likes on other page just use the next code instead.

    COPY CODE
    
    add_filter( 'pre_get_posts', 'show_posts_by_kleo_likes' );
    function show_posts_by_kleo_likes( $query ) {
        if(is_page(array ( 'mypage-slug', 'my-second-page-slug', 'thirdpage-slug' ) )) {
            $query->set('meta_key', '_item_likes');
            $query->set('orderby', 'meta_value_num');
            $query->set('order', 'DESC');
            return $query;
        }
    }
    

    So the post will be sort only in those page slugs ‘mypage-slug’, ‘my-second-page-slug’, ‘thirdpage-slug’ eddit those as you need.

    Cheers
    R

    Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solution
    #171064
     kffaner
    Participant

    i dont understand, if it does not give the option, then how do i know what to choose or when it will do this? it will override any option i choose on the widget, if i define the page slug on the code?

    #171065
     kffaner
    Participant

    i want to use widgets to display posts by likes and another to display by publish date. so if i use this code and it overrides any options i set on a particular page, then it will sort all of them by likes?

    #171287
     Radu
    Moderator

    Hi,

    Ah I see,

    The snippet just arranges the posts that are displayed on the blog page.. but this will not work if you want to sort a loop by likes and another one by something else.

    For the moment there is no quick solution to add the sort option by likes in the visual composer posts element.

    Cheers
    R.

    Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solution
    #171298
     kffaner
    Participant

    i strongly suggest adding that feature to the theme. Its something that there arent a lot of out there (for whatever reason) and its something that pretty much everyone using this theme would be able to incorporate and would do so right away. It makes little sense to have a function for “likes” but no function to sort posts by those likes.

    #171425
     Radu
    Moderator

    Hi,

    I will take your suggestion and I will add it to our improvement list when we do something we will let you know

    Cheers
    R.

    Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solution
Viewing 20 posts - 1 through 20 (of 20 total)

You must be logged in to reply to this topic.

Log in with your credentials

Forgot your details?