• Hi,

    I use this code to show the post score which bcworkz made for me:

    <?php
    $counts = aigoo_get_score();
    extract( $counts );  //restores compacted variables
    
    <span>+'.$score.'</span>
    ?>

    This is in my functions.php:

    <?php
    /* Display likes/unlikes */
    function aigoo_get_score() {
       $comments = get_comments( array('post_id' => get_the_ID(),) );
    			$likes = array();
    			$unlikes = array();
    			foreach( $comments as $comment ) {
    				if('unlike' == $comment->comment_content ) $unlikes[] = $comment;
    					else $likes[] = $comment;
    			}
        $likescore = count( $likes );
    	$unlikescore = count( $unlikes );
    	$score = count($likes) - count($unlikes);
    	$totalvotes = count($likes) + count($unlikes);
    	return compact('likes', 'unlikes', 'likescore', 'unlikescore', 'score', 'totalvotes');
    }
    ?>

    Now comes the new part of code I just use. The following code is showing the latest 5 posts with the most comments in a specific category on my homepage:

    <?php
    			$popular = new WP_Query( array(
    				'post_type'             => array( 'post' ),
    				'showposts'             => 5,
    				'cat'                   => 'activiteit',
    				'ignore_sticky_posts'   => true,
    				'orderby'               => 'comment_count',
    				'order'                 => 'dsc',
    				'date_query' => array(
    					array(
    						'after' => '0',
    					),
    				),
    			) );
    		?>
    		<?php while ( $popular->have_posts() ): $popular->the_post(); ?>
    			<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a> (<?php comments_number( 'Nu stemmen!', '1', '%' ); ?>)</li>
    		<?php endwhile; ?>

    How can I make it happen that instead of showing the posts with the most comments it shows the posts with the highest voted comments on my homepage? (See the first and second code.)

    I don’t know if it’s even possible. Hopefully someone can help me with this.

    Problem 2:
    I also notice that when I duplicate this code and change the categoryname it lists both only one category even when the category name is different.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter edow

    (@edow)

    I changed the query to:

    <?php
    			$popular_activiteit = new WP_Query( array(
    				'post_type'             => 'post',
    				'posts_per_page'        => 5,
    				'category_name'         => 'activiteit',
    				'ignore_sticky_posts'   => true,
    				'orderby'               => 'comment_count',
    				'order'                 => 'DESC',
    				'date_query' => array(
    					array(
    						'after' => '0',
    					),
    				),
    			) );
    		?>
    		<?php if ( $popular_activiteit->have_posts() ) : ?>
    		<?php while ( $popular_activiteit->have_posts() ): $popular_activiteit->the_post(); ?>
    			<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a> (<?php comments_number( 'Nu stemmen!', '1', '%' ); ?>)</li>
    		<?php endwhile; ?>
    		<?php wp_reset_postdata(); ?>
    
    		<?php else:  ?>
    
    		<p><?php _e( 'Sorry, er gaat even iets fout. We hebben niets kunnen vinden.' ); ?></p>
    
    		<?php endif; ?>

    The first query works, but when I try three of these queries (with other category names) it only shows the post of the first query.

    Also, I don’t know a solution for the “order by score” function yet. Still trying.

    Thread Starter edow

    (@edow)

    This is working for the loops now:

    <?php
    			$popular_restaurants = new WP_Query( array(
    				'post_type'             => 'restaurant',
    				'posts_per_page'        => 5,
    				'category_name'         => 'restaurants',
    				'ignore_sticky_posts'   => true,
    				'orderby'               => 'comment_count',
    				'order'                 => 'DESC',
    				'date_query' => array(
    					array(
    						'after' => '0',
    					),
    				),
    			) );
    		?>
    		<?php if ( $popular_restaurants->have_posts() ) : ?>
    		<?php while ( $popular_restaurants->have_posts() ): $popular_restaurants->the_post(); ?>
    			<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a> (<?php comments_number( 'Nu stemmen!', '1', '%' ); ?>)</li>
    		<?php endwhile; ?>
    		<?php wp_reset_postdata(); ?>
    
    		<?php else:  ?>
    
    		<p><?php _e( 'Sorry, er gaat even iets fout. We hebben niets kunnen vinden.' ); ?></p>
    
    		<?php endif; ?>

    Now I only have the question about how to deal with the most voted posts instead of showing the most commented posts.

    Moderator bcworkz

    (@bcworkz)

    Hi edow, as you might imagine, I’m catching up on established threads after my travels. I saw esmi closed your latest question in a previous thread, which led me here.

    I’m not sure I understand what you are trying to do because, as I understand your site, the comments are votes only, they are never true comments. Thus I believe the most voted posts are the same as most commented posts. Since you are not getting the desired results, I must be missing something, you may need to explain further.

    As a guess, I think you are wanting the posts with the highest scores, the difference between up and down votes, instead of just the most votes overall. This is possible, but gets more complicated since the code must specifically call aigoo_get_score() for every post in turn, and keep track of the results to determine which posts meet all the criteria. This has the effect of loading up PHP to do the work normally done by SQL, in addition to causing very many comment queries to be run for merely 5 results. This is very inefficient, especially as the site grows.

    You could go ahead and develop some brute force code to do this, but the real solution is to establish some sort of score maintenance scheme so that SQL can return the results needed with a single query, taking the load off PHP.

    This requires further thought, but I am not going to commit any more thought until I’m sure I understand what your real desire is.

    Thread Starter edow

    (@edow)

    Hi! I hope you had a great time again. I see you’re talking about traveling often, which sounds good 🙂

    I’m sorry I wasn’t clear the first time. Indeed I want a top 5 of posts with the highest score for each category. I was wondering if it was easy to make, but it sounds like a lot of work which, with my skills, will be hard to achieve.

    Maybe you can show me some hints, so I can look if it’s doable for me.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to show the posts with the highest votes on homepage’ is closed to new replies.