Support » Plugin: Like Button Rating ♥ LikeBtn » Sort archive page

  • Resolved YuriV

    (@yuriv)


    Hi all,

    We purchased this plugin after we read in the documentation that it’s possible to sort posts by likes, using this code:

    <?php
    $query_args  = array(
        'post_type' => 'post',
        'orderby' => 'meta_value',
        'order' => 'DESC',
        'meta_query' => array(
            'relation' => 'OR',
             array(
                'key' => 'Likes',
                'compare' => 'NOT EXISTS',
                'type' => 'numeric'
             ),
             array(
                'key' => 'Likes',
                'compare' => 'EXISTS',
                'type' => 'numeric'
             )
         )
    );
    $loop = new WP_Query($query_args);
    if ($loop->have_posts()):
        while($loop->have_posts()):
            $loop->the_post();
    ?>
            <a href="<?php the_permalink(); ?>">
                <div>
                    <h3><?php echo the_title();?></h3>
                    <p><?php echo the_content(); ?></p>
                </div>
            </a>
    <?php
        endwhile;
    endif;
    ?>

    The documentation said this code could be placed on for example archive.php. We placed the code there and it does return the posts by likes, but the problem is it returns all posts as a top list, and not only the posts with the specific tag we’re in. When we visit:

    http://site.com/tag/1

    We of course only want to see the content tagged ‘1’. Support returned a short mail containing:

    Tweak WP_Query code - Follow instructions to add tag parameter to WP_Query: https://codex.wordpress.org/Class_Reference/WP_Query

    Unfortunately we can’t make any sense of these instructions. Does anybody know how to tweak the code above to make it return only posts with the same tag?

    Thanks in advance,

    Yuri

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author LikeBtn

    (@likebtn)

    Hi,

    What kind of loop do you have in archive.php?

    If it is have_posts() you need to insert the following before the loop:
    <?php query_posts($query_string . ‘&meta_key=Likes&orderby=meta_value&meta_type=numeric&order=DESC’); ?>

    Read more here: https://likebtn.com/en/wordpress-like-button-plugin#sort_posts_by_likes

    Thanks. Problem is solved.

    Just found there is a mayor problem with this. The given codes shows the items in the right order (top voted on top) but items with no votes at all or not visible at all anymore in the archive-page…

    See for example: https://citaten-en-wijsheden.nl/naam/jan-greshoff

    This achive had 3 items but they’re not visible because they didn’t receive any votes… Do you know how to solve this?

    I did some further research on above issue:

    In WordPress we can select the extra field ‘Likes’ and manual add ‘0’ to new posts. Once we’ve done that the post is also visible on the archive pages with top rated content.

    But of course we can’t go through all articles and manually add ‘0’ to the unrated posts. The plugin should do this.

    Hope you can solve this quickly because at this moment all our new posts aren’t visible………….

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.