Support » Plugin: WP ULike » How would I track likes by the month?

  • Resolved Reppiks

    (@reppiks)


    Greetings, I would like to be able to track and show the most liked uploaded content for the month. I am using MediaPress for adding upload capabilities to my BuddyPress site. Really I need the previous month.
    I would like to offer a contest/reword for the user who gets the most likes for the month on my site. I see in the FAQs the below code for listing the most liked post.

    ‘published’,
    ‘post_type’ =>’post’,
    ‘orderby’ => ‘meta_value_num’,
    ‘meta_key’ => ‘_liked’,
    ‘paged’ => (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1
    ));
    ?>

    I am no programmer so I wouldn’t know how to sort this info for a given time period. I am looking through the codex now and seeing if I can possible connect what’s going on to achieve this. I’m not sure what (‘post_type’) uploaded content is via MediaPress. Can anybody help me with what I need to do to achieve this?

    • This topic was modified 1 month, 1 week ago by Reppiks.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Reppiks

    (@reppiks)

    ::: Update :::

    So I now know what to add to the above code to get the results for the previous month.

    I just need to add something like this to the array:
    'monthnum' => 4,

    Right now I don’t mind manually changing the month number every month and I should be able to write up something that can fetch the current month – 1 to have it auto updated. (Will be some PHP practice for me, but if somebody wants to share a script that’ll do it I’m not against it.)

    What I am still stuck at is having the info bring up “posts” made by MediaPress. I am using WP ULike with MediaPress content following the tutorial on this page: http://buddydev.com/mediapress-development/mediapress-like-unlike-button-for-photos-videos-etc-with-wp-ulike-plugin/

    WP ULink tracks the likes just fine and I can even see the post_id and the attachment page of the media in the stats of WP ULike. My problem is that wordpress doesn’t treat the Mediapress post as any type of post_type, (or at least I don’t know whay type) so the above code only works for post, pages, etc but not MediaPress post. (Even when post_type is set to any)

    I believe the solution for me will lie in how WP Ulike gets it’s stats info. It is able to track a post_id as well as attachment page so there must be a way this info is queried by the plug-in. Again I am no PHP programmer and can only read it a bit so digging out what I need from the plug-in’s editable files is proving difficult. How does WP ULike get it’s stats?

    Reppiks

    (@reppiks)

    ::: Update :::

    So I almost have it 100% figured out. I know how to get the most liked media to show. I needed to use mpp_media_query instead of wp_query and use some of the properties and methods that were added by MediaPress.

    Here is what my working code looks like:

    <?php
        $args = array(
            //'monthnum' => 1,
            'meta_key' => '_liked',
            'orderby' => 'meta_value_num',
    
        );
    
    // The Query
    	$the_media_query = new MPP_Media_Query( $args );
     
    // The Loop
    if ( $the_media_query->have_media() ) {
    
        while ( $the_media_query->have_media() ) {
            $the_media_query->the_media();
    	?>
    		<p text-align="center">
    		<a href="<?php mpp_media_permalink() ;?>" <?php mpp_media_html_attributes( array( 'class' => 'mpp-item-thumbnail mpp-media-thumbnail mpp-photo-thumbnail' ) ); ?>>
            	<img class="aligncenter" src="<?php mpp_media_src( 'thumbnail' ) ;?>" alt="<?php echo esc_attr( mpp_get_media_title() );?> "/>
    		</a></p>
            
            <?php
        }
    
    } else {
        // no posts found
    }
    /* Restore original media data */
    mpp_reset_media_data();
    ?>
    

    The main problem! For post, media, etc.
    This still just list most liked with no date range option. So it will show the most liked media period, from all time.
    I still need to know how to set a time range for the query. Putting the monthnum argument in (like I did in my previous post) doesn’t list the post by the date they were liked, but by when they were published.

    WP Ulike tracks the time and date of likes in the statistics. How would I get this data for use in a query of post?
    (Example: Query post with meta-key = _liked from Jan to Feb)

    Plugin Author Alimir

    (@alimir)

    Hi @reppiks
    Maybe the following code helps you to resolve this problem. 🙂

    $start = date('Y'.$month.'01'); // First day of the month
    $end = date('Y'.$month.'t'); // 't' gets the last day of the month
    $args = array(
        'orderby' => 'meta_value_num',
        'meta_key' => '_liked',
        'meta_query' => array(
            array(
                'key' => '_liked',
                'value' => array($start, $end),
                'compare' => 'BETWEEN',
                'type' => 'DATE'
            )
        )
    );
    • This reply was modified 3 weeks, 6 days ago by Alimir.

    Hello agian @alimir

    The code you gave isn’t working for me. The query doesn’t seem to respond to the date. At least not in that format. Here is what I got going right now.

    I wanted to get the previous months and the follow format is the only way that works and keeps the right amount of days for the month. Using your code and providing the month produced results where the month would be right but the first and last days would be based on the current month.

    
    date_default_timezone_set('America/Chicago');
    
    <?php
    $start = date('Y.n.d', strtotime("first day of -1 month")); // First day of the month
    $end = date('Y.n.d', strtotime("last day of -1 month")); // 't' gets the last day of the month
    
    $args = array(
        'meta_key' => '_liked',
        'gmeta_query' => array(
            array(
                'key' => '_liked',
                'value' => array($start, $end),
                'compare' => 'BETWEEN',
                'type' => 'DATE'
            )
        )
    );
    
    // The Query
    	$the_media_query = new MPP_Media_Query( $args );
     
    // The Loop
    if ( $the_media_query->have_media() ) {
    
        while ( $the_media_query->have_media() ) {
            $the_media_query->the_media();
    	?>
    		<p text-align="center">
    		<a href="<?php mpp_media_permalink() ;?>" <?php mpp_media_html_attributes( array( 'class' => 'mpp-item-thumbnail mpp-media-thumbnail mpp-photo-thumbnail' ) ); ?>>
            	<img class="aligncenter" src="<?php mpp_media_src( 'thumbnail' ) ;?>" alt="<?php echo esc_attr( mpp_get_media_title() );?> "/>
    		</a></p>
    
    <p>Number of Likes: <?php
    if (function_exists('wp_ulike_get_post_likes')):
    	echo wp_ulike_get_post_likes(get_the_ID());
    endif;
    ?></p>
    
    <?php echo '<a href="'.get_permalink($get_ulike_log->post_id).'" title="'.get_the_title($get_ulike_log->post_id).'">'.get_the_title($get_ulike_log->post_id).'</a>'; ?>>
    

    I also tried to setup a similar code to just bring up post and not mediapress content in case that was the problem, and still got the same results. (Using regular wp_query and meta_query) No matter what date is given it just shows all post that have likes.

    This issue is not resolved by the way! Not sure how that happened, is there a way you can un-resolve it Alimir?

    Ok the status is set back right.

    • This reply was modified 2 weeks, 3 days ago by Reppiks.

    I have come up with a solution and am sharing in case anybody else would like to use it. Thanks again Alimir, what you provided before lead me in a great direction. I am a novice in PHP and never learned SQL so this journey has been quite the learning experience.

    Sort post/media by total like count for the previous month:
    First, I set up args for the begin time and the end time I want to query for.

    The code below set’s current timezone for dates to be based off of. (I’m in Memphis so mine is America/Chicago…replace with yours) Otherwise it would use the timezone of the server that’s hosting the page.
    date_default_timezone_set('America/Chicago');

    I set up the start and end times like this because having the way that was suggested above would not actually count the days of the month but base the days off the current month. I also chose this date format because it was how the time_date was displayed in the database table.

    $start = date('Y-m-d 00:00:00', strtotime("first day of -1 month")); // First day of the month
    $end = date('Y-m-d 23:59:59', strtotime("last day of -1 month")); // 't' gets the last day of the month

    The info we needed I believe is beyond the scope of wp_query. (Because I needed to access the _ulike table and columns) So I used $qpdb to do a query directly to the database. Through a lot of trial and error this is what I came up with this:

    global $wpdb;
    $like_counts = $wpdb->get_results( "
    	SELECT post_id, COUNT(post_id) as like_number, status
    	FROM {$wpdb->prefix}ulike
    	WHERE {$wpdb->prefix}ulike.date_time >= '$start' 
    	AND {$wpdb->prefix}ulike.date_time <= '$end'
    	AND {$wpdb->prefix}ulike.status = 'like'
    	GROUP BY {$wpdb->prefix}ulike.post_id 
    	ORDER BY COUNT(post_id)
    	DESC ");
    
     foreach ($like_counts as $like_count)

    That query counts how many times a post occur in the query between the dates above and groups them by the post id.

    I then did a MPP_Media_Query (which is similar to WP_Query) so that it would only pull uploaded media instead of media and post, and set id = $like_count->post_id as the only argument for it.

    • This reply was modified 3 days, 17 hours ago by Reppiks.
Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.