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 am using widget-reloaded plugin and i have a function which count post views.

<?php
// function to display number of posts.
function tie_views( $postID = '' ){
    if( !tie_get_option( 'post_views' ) ) return false;

    global $post;
    if( empty($postID) ) $postID = $post->ID ;

    $count_key = 'tie_views';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        $count = "0";
    }
    return '<span class="post-views">'.$count.' '.__( 'Views' , 'tie' ).'</span> ';
}

// function to count views.
function tie_setPostViews() {
    if( !tie_get_option( 'post_views' ) ) return false;

    global $post;
    $postID = $post->ID ;
    $count_key = 'tie_views';
    $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);
    }
}


// Add it to a column in WP-Admin 
add_filter('manage_posts_columns', 'tie_posts_column_views');
add_action('manage_posts_custom_column', 'tie_posts_custom_column_views',5,2);
function tie_posts_column_views($defaults){
    $defaults['tie_post_views'] = __('Views');
    return $defaults;
}
function tie_posts_custom_column_views($column_name, $id){
    if($column_name === 'tie_post_views'){
        echo tie_views(get_the_ID());
    }
}
?>

Now i have to sort categories according to post views, the post views give total views per post and its query like select category from wp_category order by post_views, either i can use this query directly in wp_list_categories and pass other params like exclude params etc but that can be done manually. I'll try to edit plugin file and add option in form it shows in form interface but didn't found how to activate new argument in form. Presently widget can sort by name, ID, count, slug. This is widget snippet code:

/* <select> element options. */
        $taxonomies = get_taxonomies( array( 'show_tagcloud' => true ), 'objects' );
        $terms = get_terms( $instance['taxonomy'] );
        $style = array( 'list' => __( 'List', $this->textdomain ), 'none' => __( 'None', $this->textdomain ) );
        $order = array( 'ASC' => __( 'Ascending', $this->textdomain ), 'DESC' => __( 'Descending', $this->textdomain ) );
        $orderby = array( "tie_views()" => __( 'Views', $this->textdomain ), 'count' => __( 'Count', $this->textdomain ), 'ID' => __( 'ID', $this->textdomain ), 'name' => __( 'Name', $this->textdomain ), 'slug' => __( 'Slug', $this->textdomain ), 'term_group' => __( 'Term Group', $this->textdomain ) );
        $feed_type = array( '' => '', 'atom' => __( 'Atom', $this->textdomain ), 'rdf' => __( 'RDF', $this->textdomain ), 'rss' => __( 'RSS', $this->textdomain ), 'rss2' => __( 'RSS 2.0', $this->textdomain ) );

In $orderby, i added new argument Views in array with key name as function which count views. I am not able to understand this very much, please help me in this issue.

share|improve this question
add comment

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.