WordPress.org

Ready to get started?Download WordPress

Forums

LikeBtn Like Button
[resolved] Display most popular with WP_Query (4 posts)

  1. ttmt
    Member
    Posted 3 months ago #

    Hi all

    Is it possible to display the most popular posts using a WP_Query

    This is the code I'm using to display posts.

    <?php
    $idea_args  = array(
    	'post_type' => 'post',
    	'order' => 'ASC',
    	'post_status' => 'delivered'
    );
    
    $idea_loop = new WP_Query($idea_args);
    
    if($idea_loop->have_posts()):
    	while($idea_loop->have_posts()):
    		$idea_loop->the_post();
    
    ?>
    
    <a href="<?php the_permalink(); ?>">
    	<div class="ideasContainer__idea">
    		<h3><?php echo the_title();?></h3>
    		<p><?php echo the_content(); ?></p>
    	</div>
    </a>	
    
    <?php
    endwhile;
    endif;
    ?>
    
    <?php wp_reset_postdata(); ?>

    I would like to create this html structure but for the most popular posts

    <a href="<?php the_permalink(); ?>">
    	<div class="ideasContainer__idea">
    		<h3><?php echo the_title();?></h3>
    		<p><?php echo the_content(); ?></p>
    	</div>
    </a>

    Can I query the Like Button in the query or how would I create this html structure while displaying the most popular post.

    https://wordpress.org/plugins/likebtn-like-button/

  2. likebtn
    Member
    Plugin Author

    Posted 3 months ago #

    There is a recipe for query_posts function:
    http://likebtn.com/en/wordpress-like-button-plugin#sort_posts_by_likes

    We will try to find a solution for WP_Query and let you know.

  3. likebtn
    Member
    Plugin Author

    Posted 3 months ago #

    See instructions on http://likebtn.com/en/wordpress-like-button-plugin#sort_posts_by_likes

    <?php
    $query_args  = array(
        'post_type' => 'post',
        'orderby' => 'meta_value',
        'order' => 'DESC',
        'meta_query' => array(
            'relation' => 'OR',
             array(
                'key' => 'Likes',
                'compare' => 'NOT EXISTS'
             ),
             array(
                'key' => 'Likes',
                'compare' => 'EXISTS'
             )
         )
    );
    $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;
    ?>
  4. ttmt
    Member
    Posted 3 months ago #

    Thank you likebtn, thats brilliant

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.