Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have three sections:

1-New posts 2-Trending posts 3-Hot Posts

I'm using a Vote plugin and i sort every section like this:

New Posts:

$args = array(
  'meta_query'=> array(
    array(
      'key' => '_wti_like_count',
      'compare' => '<=',
      'value' => 5,
      'type' => 'numeric',
  'posts_per_page' => 5
) ) );
query_posts( $args );

The result of this is:

SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) WHERE 1=1 AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') AND wp_postmeta.meta_key = '_wti_like_count' AND wp_postmeta.meta_value <= '5' GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 100

On this section only the posts from 0 to 5 Votes are displaying.

Once it Reach 6 Votes .. It goes to the Trending section ->

$args = array(
  'meta_query'=> array(
    array(
      'key' => '_wti_like_count',
      'compare' => '>=',
      'value' => 5,
      'type' => 'numeric',
  'posts_per_page' => 5
) ) );
query_posts( $args );

Where posts between 5 or more... are displaying an so on...

My problem its that i ORDER them BY wp_posts.post_date and once an old posts get more votes than a new one it goes from News Posts to Trending Posts but it doesn't display on TOP of the Section... It goes below..

I wan't to be able to do something like this..

$args = array(
  'meta_query'=> array(
    array(
      'key' => '_wti_like_count',
      'compare' => '<=',
      'value' => 5,
      'type' => 'numeric',
  'posts_per_page' => 5
) ) );
query_posts( $args );

when it reach to 6 votes... I want to be able to update the wp_posts.post_date to current so like that every time an old post receive more than 5 votes .. it shows first in the trending section... not below

something like if 'value' = 6 update wp_posts.post_date to current?

share|improve this question
    
You would need to add the date updating to the Vote plugin, not with the queries. When somebody votes the plugin should first check how many votes the post has and then update the post date if the post has enough votes. –  epilektric Sep 28 '13 at 7:34
    
There's got to be a way to do it with the queries... Something like : <?php if(get_post_meta($page->ID, "_wti_like_count", true)==6) { ?> { Change published date of the post to current date } Query post Please help me out!! –  Oscar Sep 28 '13 at 18:26
    
If you do that you will be constantly updating the post date for as long as the like count equals 6. The post will forever be at the top of the list. –  epilektric Sep 28 '13 at 18:57
    
That makes sense... So how can i made my query Update the post date when the like count equals 6 only once? So like that it doesn't constantly update the post date... –  Oscar Sep 28 '13 at 19:13
    
Here's a peace of my Vote Plugin so like that you have an idea of how i can do it : // Get like/dislike count $wti_like_count = GetWtiLikeCount($post_id); $wti_unlike_count = GetWtiUnlikeCount($post_id); // Update post meta update_post_meta($post_id, '_wti_like_count', (int)str_replace('+', '', $wti_like_count)); update_post_meta($post_id, '_wti_unlike_count', (int)str_replace('-', '', $wti_unlike_count)); $wti_total_count = GetWtiTotalCount($post_id); update_post_meta($post_id, '_wti_total_count', $wti_total_count); } –  Oscar Sep 28 '13 at 19:18

1 Answer 1

up vote 0 down vote accepted

You can't update the post date with the queries because the update would run repeatedly while the post count equals 6. Every time the query is called the post date would be updated.

You can, however, modify the plugin to update the post date when the like button is clicked and the conditions are met.

This is the basic code you would need to update the post date when the post count equals 6.

if ( GetWtiLikeCount($post_id) == 6 ) {
    //update date
    $time = current_time('mysql');
    wp_update_post(
        array (
            'ID'            => $post_id,
            'post_date'     => $time,
            'post_date_gmt' => get_gmt_from_date( $time )
        )
    );
}

You need to add the code into wti_like_post_ajax.php so that it will be triggered when the like button is clicked. Here it is in context with some of the surrounding code.

if ($can_vote) {
    $current_user = wp_get_current_user();
    $user_id = (int)$current_user->ID;

    if ($task == "like") {
        if ($has_already_voted) {
            $query = "UPDATE {$wpdb->prefix}wti_like_post SET ";
            $query .= "value = value + 1, ";
            $query .= "date_time = '" . date('Y-m-d H:i:s') . "' ";
            $query .= "WHERE post_id = '" . $post_id . "' AND ";
            $query .= "ip = '$ip'";
        } else {            
            $query = "INSERT INTO {$wpdb->prefix}wti_like_post SET ";
            $query .= "post_id = '" . $post_id . "', ";
            $query .= "value = '1', ";
            $query .= "date_time = '" . date('Y-m-d H:i:s') . "', ";
            $query .= "ip = '$ip'";
        }

        if ( GetWtiLikeCount($post_id) == 6 ) {
            //update date
            $time = current_time('mysql');
            wp_update_post(
                array (
                    'ID'            => $post_id,
                    'post_date'     => $time,
                    'post_date_gmt' => get_gmt_from_date( $time )
                )
            );
        }

    } else {
share|improve this answer
    
Excellent answer my dear friend! It works like a charm!! You are a life saver! I can't even VOTE UP your answer cos it says Vote UP requires 15 reputation LOL!.. THANK YOU SO MUCH!! –  Oscar Sep 28 '13 at 22:16
    
No need to vote up. You can accept the answer as the correct answer to your question though. –  epilektric Sep 29 '13 at 1:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.