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 the following function in functions.php that allows sorting alphabetically or by recency. I want to change the alphabetic to sort by most viewed. This is the code I have for the sorter, which is a dropdown select menu:

function sorter($wp_query) {

        if(mysql_real_escape_string($_REQUEST["sorting"]) == "date"){
            usort($wp_query->posts, 'cmp_date');
        }else if(mysql_real_escape_string($_REQUEST["sorting"]) == "title"){
            usort($wp_query->posts, 'cmp_alpha');
        }
}

And this is the code for the sorter:

function custom_sort(){ 
// retrieve the sorting variables
    $sort_selected = mysql_real_escape_string($_REQUEST["sorting"]); ?>
<form action="" method="post">
    <div class="sorting">
        <span>Sorting:</span>
        <select name="sorting" onchange="this.form.submit();">
        <?php switch($sort_selected){
            case "";
            case "date";
            ?>
                <option value="date">Recency</option>
                <option value="title">Alphabetic</option>
                <option value="views">Views</option>
        <?php break;
        case "title";?>
                <option value="title">Alphabetic</option>
                <option value="date">Recency</option>
                <option value="views">Views</option>
        <?php break;
        case "views";?>
                <option value="views">Views</option>
                <option value="title">Alphabetic</option>
                <option value="date">Recency</option>
        <?php break;
        } // end switch ?>
        </select>
    </div><!-- exit div sorting -->
</form> 
<?php }

And I think I need to use something like this (which I saw in another question on this site):

<?php $args = array(
      'post_status' => 'publish',
      'meta_key' => 'post_views_count',
      'orderby' => 'meta_value_num',
      'order' => 'ASC'
    );
    $the_query = new WP_Query($args); //http://codex.wordpress.org/Class_Reference/WP_Query

//the loop
while ( $the_query->have_posts() ) : $the_query->the_post();

//do your magic
endwhile;

But I don't understand how I can translate the code above into code that will fit my current function. I'm using this to get post views:

function getPostViews($postID){
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0 View";
    }
    return $count.' Views';
}
function setPostViews($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}

I hope this makes sense, and thanks in advance.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

You can use what you currently have and add a new condition:

function sorter($wp_query) {
        if(mysql_real_escape_string($_REQUEST["sorting"]) == "date"){
                usort($wp_query->posts, 'cmp_date');
        }elseif(mysql_real_escape_string($_REQUEST["sorting"]) == "title"){
                usort($wp_query->posts, 'cmp_alpha');
        }elseif(mysql_real_escape_string($_REQUEST["sorting"]) == "views"){
                //create a list of all post ids as an array
                $ids = array();
                foreach ($wp_query->posts as $p) {
                        $ids[] = $p->ID;
                }
                //the query these posts again and sort them by views
                $args = array(
                      'post_status' => 'publish',
                      'meta_key' => 'post_views_count',
                      'orderby' => 'meta_value_num',
                      'order' => 'ASC',
                      'post__in' => $ids
                );
                $the_query = new WP_Query($args);
                return $the_query;
        }
}
share|improve this answer
    
Thanks for that! It doesn't seem to be working though. –  Donna May 22 '12 at 13:28
    
OK I added function cmp_views($a, $b){ return strcasecmp($a->post_views_count, $b->post_views_count); } and that is almost working, its not quite in order though. –  Donna May 22 '12 at 13:42
    
I'm not sure that will work since post_views_count is not a property of the post object, since it's stored in the postmeta table, and that is why the new query is called. the question is how are you calling the sorter function? –  Bainternet May 22 '12 at 14:26
    
sorter($wp_query); $results_count = $wp_query->post_count; –  Donna May 22 '12 at 14:33
    
I got this code from another post, could I somehow use this function to alter the cmp_views? function posts_column_orderby( $vars ) { if ( isset( $vars['orderby'] ) && 'post_views' == $vars['orderby'] ) { $vars = array_merge( $vars, array( 'meta_key' => 'post_views_count', 'orderby' => 'meta_value_num' ) ); } return $vars; } add_filter( 'request', 'posts_column_orderby' ); –  Donna May 22 '12 at 15:36
show 6 more comments

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.