Viewing 16 posts - 1 through 16 (of 16 total)
  • Author
    Posts
  • #9946

    davidanielyan
    Participant

    Hi. I have to show “HOT” icon if the post enter into the today most viewed 10 posts.
    Is there any way or alternative solution ?

    #9947

    Bartosz
    Keymaster

    Hi,

    Since recent plugin version there is a way to do this. You just need to perform a query to get the today’s hot posts, like that:

    $hot_args = array(
    	'post_type'			=> array( 'post' ),
    	'posts_per_page'	=> 10,
    	'orderby'			=> 'post_views',
    	'suppress_filters'	=> false,
    	'views_query'		=> array(
    		'year'	=> date( 'Y' ), // this year
    		'month' => date( 'm' ), // this month
    		'day'	=> date( 'd' ), // this day
    	)
    );
    
    $hot = get_posts( $hot_args );

    When you have that you’ll just need to compare if your current post is in array of today’s hot posts.

    Hope this helps.

    #9948

    davidanielyan
    Participant

    Dear friend,

    It is my code how can I integrate your peace of code inside my post query,

    
    <?php
    $args = array(
    'post_type'		=>	'post',
    'showposts'		=>	45,
    'order' 		=> 	'DESC',
    'orderby' 		=> 	'date'
    );
    $the_query = new WP_Query( $args ); 
    if( $the_query->have_posts() ) : while( $the_query->have_posts() ) : $the_query->the_post();
    $feat_image_news = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); 
    $post_permalink = get_post_permalink();
    $content = get_the_content();	
    $paragraph_first_strip = strip_shortcodes($content);	
    $paragraph_first = substr($paragraph_first_strip, 0, 100);
    ?>
    <a class="videoInfo articlesInfoA columns">
    <div class="articlesImages" style="background-image:url(<?php echo $feat_image_news; ?>);"></div>
    <div class="articlesInfo ">
    <h6 class="topInfo fontCustom">
    	<div class="heightTopInfo">
    		<div class="floatLeft"><?php the_title(); ?></div>
    		<div class="floatRight">
    			<span class="tagHot"></span>
    			<span class="tagExclusive"></span>
    		</div>
    	</div>
    	<div>
    		<span class="sportType tagBasketball">
    
    		</span>
    		<span class="newsDate"><?php echo get_the_date('d/m/Y'); ?><?php //echo pvc_get_post_views( $post->ID ) ?></span>
    	</div>
    </h6>
    <div class="newsContentTextInfo ">
    	<?php echo $paragraph_first; ?>
    </div>
    </div>
    </a>
    <?php 
    endwhile;
    wp_reset_postdata(); 
    ?>
    <?php else : ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; ?>
    
    

    Inside my code you can find “<span class=”tagHot”></span>”. I need to display it if post is one of the today most viewed.

    #9949

    Bartosz
    Keymaster

    You would have to perform the query above first to get the today’s hot posts and then compare the returned posts and their ID’s with the ID of the current post.

    Sth like:

    // there is a hot posts query above
    
    // now get the hot post's ID's
    $hot_posts_ids = array();
    
    foreach ( $hot as $hot_post ) {
    	$hot_posts_ids[] = $hot_post->ID;
    }
    
    // your query starts here
    
    // inside your custom query
    if ( in_array( $post->ID, $hot_posts_ids) ) {
    	// i'm hot
    }

    Good luck,

    #9953

    davidanielyan
    Participant

    OK.. So as you said I did. But it’s doesn’t work.. please can you see is there any mistake ?

    
    $hot_args = array(
    	'post_type'			=> array( 'post' ),
    	'posts_per_page'	=> 10,
    	'orderby'			=> 'post_views',
    	'suppress_filters'	=> false,
    	'views_query'		=> array(
    		'year'	=> date( 'Y' ), // this year
    		'month' => date( 'm' ), // this month
    		'day'	=> date( 'd' ), // this day
    	)
    );
    
    $hot = get_posts( $hot_args );
    
    $hot_posts_ids = array();
    
    foreach ( $hot as $hot_post ) {
    	$hot_posts_ids[] = $hot_post->ID;
    }
    
    $args = array(
    	'post_type'		=>	'post',
    	'showposts'		=>	5,
    	'order' 		=> 	'DESC',
    	'orderby' 		=> 	'date',
    	);
    
    $the_query = new WP_Query( $args ); 
    	if( $the_query->have_posts() ) : while( $the_query->have_posts() ) : $the_query->the_post();
    
    ...
    
    //AND INSIDE LOOP 
    <?php if ( in_array( $post->ID, $hot_posts_ids) ) { ?>
    	<span class="tagHot"></span>
    <?php } ?>
    <?php 
    	endwhile;
    	wp_reset_postdata(); 
    	?>
    <?php else : ?>
    	<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; ?>
    
    #9954

    Bartosz
    Keymaster

    For me this is not correct what your doing inside your query, please check https://codex.wordpress.org/Class_Reference/WP_Query

    I think this line should be:

    //AND INSIDE LOOP 
    <?php if ( in_array( $the_query->post->ID, $hot_posts_ids) ) { ?>
    #9955

    davidanielyan
    Participant

    OK, I fix the line, but before that when code had:
    <?php if ( in_array( $post->ID, $hot_posts_ids) ) { ?>
    In All displayed posts only in first 10 posts showed <span class=”tagHot”></span> it’s not correct I know. But after change to your code
    <?php if ( in_array( $the_query->post->ID, $hot_posts_ids) ) { ?>
    now is nothing show. I didn’t know what to do.

    #9956

    Bartosz
    Keymaster

    I’m sorry, got lost. You have everything needed above, just use it – we can’t help you further.

    #9957

    davidanielyan
    Participant

    I understand you but I see that problem is gone from your wrote code

    
    $hot_args = array(
    			'post_type'			=> array( 'post' ),
    			'posts_per_page'	=> 10,
    			'orderby'			=> 'post_views',
    			'suppress_filters'	=> false,
    			'views_query'		=> array(
    				'year'	=> date( 'Y' ), // this year
    				'month' => date( 'm' ), // this month
    				'day'	=> date( 'd' ), // this day
    			)
    		);
    
    		$hot = get_posts( $hot_args );
    
    $hot_posts_ids = array();
    
    		foreach ( $hot as $hot_post ) {
    			$hot_posts_ids[] = $hot_post->ID;
    		}
    

    because when I do ‘var_dumb($hot_posts_ids)’ I see that inside array has only the last 10 posts (not most viewed posts)

    #9958

    Bartosz
    Keymaster

    That’s what you wanted – topic title: “Check today most viewed 10 posts.”
    and it’s here:

    ‘posts_per_page’ => 10,`

    #9959

    davidanielyan
    Participant

    Dear Bartosz I’m sorry for causing trouble, but my invocation is that I need to include only that posts id’s which has the most views in all existing posts, but problem is that in our situation your given array has only the last published 10 posts.

    #9960

    Bartosz
    Keymaster

    The function i wrote above is to get 10 most popular posts today. If you want to have all posts ordered by today’s popularity just set ‘posts_per_page’ parameter to -1

    #9961

    davidanielyan
    Participant

    Dear friend, your wrote function doesn’t get 10 most popular posts today, It’s get last 10 published posts.

    #9962

    Bartosz
    Keymaster

    Did you check the vardump on the query results?

    #9963

    davidanielyan
    Participant

    Yes I checked vardump and there are the last published posts ID’s

    #9991

    Bartosz
    Keymaster

    I’m not sure but the views_query WP_Query parameter might not be finished. Contact us via contact for – I’ll reply to you and send a development version of the plugin that should definitely work.

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