Support » Plugin: I Recommend This » meta value _recommended

  • Resolved onz123


    Hello, thanks for this great plugin.

    I already found the meta_key _recommended in which the likes are stored. Now I’d like to know if there’s a way that the likes are written to this key _recommended with leading zeros? So for example after 12 likes the posts _recommended meta_value would be 0012, instead of 12.

    Why do I need this? To sort my posts by this meta_value. (I know that there’s also the possibility with the wordpress command sortby=meta_value_num – but this command is bugged so I need to do the sortby=meta_value)

    Help would be very appreciated

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Harish Chouhan


    That is interesting. Honestly speaking I have no clue as of now how to do this. It would probably involve adding an additional query to add 00 to the count before saving it in the database and then to also remove the 00 before displaying it.

    Could you not use this:

    meta_key='_recommended' ORDER BY $wpdb->postmeta.meta_value+0 DESC??

    Hey, I did that and it works fine. $mylikes = sprintf("%04s", $recommended); and ltrim($recommended, '0') – thanks

    Yes, I could just use the meta_value+0, but that does the same thing as meta_value_num. And I don’t know why, but the meta_value_num or meta_value+0 always creates some duplicate posts on my website and messes up the post order. The normal orderby=meta_value however works fine.

    Plugin Author Harish Chouhan


    Glad to know you found a way. Would you mind sharing a complete code here?

    Well I just changed the part with update_post_meta to write the value with leading zeros to the database.

    $myfblikes = sprintf("%04s", $recommended);
    update_post_meta($post_id, '_recommended', $myfblikes);
    setcookie('dot_irecommendthis_'. $post_id, time(), time()+3600*24*365,'/');

    Then I changed the output to remove the leading zeros again. So that you don’t have “0001 People recommend this post”
    $output = '<span class="dot-irecommendthis-count">'. ltrim($recommended, '0') .'</span> <span class="dot-irecommendthis-suffix">'. $suffix .'</span>';

    To sort by the meta_value I simply use this:

    if (have_posts()): while (have_posts()) : the_post(); ?>

    Hey do you know how I can have a URL opened at the same time when the vote is done? I tried by replacing the “#” in this line with the URL I want to open but it won’t work.

    $dot_irt_html = '<a href="#" class="'. $class .'" id="dot-irecommendthis-'. $post_ID .'" title="'. $title .'">;'
    return $output;

    Plugin Author Harish Chouhan


    Thanks a tonne @nz123 for sharing that.

    Regarding your question, you would have to to reply on JavaScript onClick to open a new URL.

    Plugin Author Harish Chouhan


    @nz123 Hi again.

    First want to apologize for not understanding your question. Only when I was writing code again I realized that you were using wp_query and not raw query like I had done.

    I finally did use this

    'orderby' => 'meta_value_num',
    'meta_key' => '_recommended',

    meta_value_num worked for me without any issue. Can you let me know what bug you were referring too.

    Not urgent just want to learn more.

    When I used orderby=meta_value_num, it happened that some posts were displayed more than one time. So for example I went through the pagination, and 1 post appeared at page 2 and then again at page 4. I don’t know any other people who had this happening – really strange. It might be my website that has 700+ posts. Anyway, with orderby=meta_value and the values with leading zeros it worked.

    Hey I’m really trying to get this working: I want that when I click on the recommendation button in the loop, that at the same time it opens the selected post. I managed to have it open another URL with JavaScript onClick: `function open_win()
    but this won’t work with<?php the_permalink(); ?>)

    You have any idea how to do this?

    Plugin Author Harish Chouhan


    No idea about that about the javascript part. But will keep it in mind.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘meta value _recommended’ is closed to new replies.