WordPress.org

Ready to get started?Download WordPress

Forums

I Recommend This
[resolved] meta value _recommended (9 posts)

  1. onz123
    Member
    Posted 11 months ago #

    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

    http://wordpress.org/plugins/i-recommend-this/

  2. Harish Chouhan
    Member
    Plugin Author

    Posted 11 months ago #

    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??

  3. onz123
    Member
    Posted 11 months ago #

    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.

  4. Harish Chouhan
    Member
    Plugin Author

    Posted 11 months ago #

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

  5. onz123
    Member
    Posted 11 months ago #

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

    $recommended++;
    $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:

    <?php
      query_posts('&orderby=meta_value&meta_key=_recommended&paged='.$paged.'');
    
    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;
  6. Harish Chouhan
    Member
    Plugin Author

    Posted 11 months ago #

    Thanks a tonne @nz123 for sharing that.

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

  7. Harish Chouhan
    Member
    Plugin Author

    Posted 11 months ago #

    @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.

  8. onz123
    Member
    Posted 11 months ago #

    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()
    {
    window.open("http://url.com")
    }`
    but this won't work with window.open(<?php the_permalink(); ?>)

    You have any idea how to do this?

  9. Harish Chouhan
    Member
    Plugin Author

    Posted 11 months ago #

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

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.