在unctions.php中按getPostView对帖子进行排序

时间:2012-05-22 作者:Donna

我在函数中有以下函数。php,允许按字母顺序或按近距排序。我想把字母顺序改为按浏览量最大的排序。这是我为分拣机准备的代码,它是一个下拉选择菜单:

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\');
        }
}
这是分拣机的代码:

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 }
我想我需要这样做(我在这个网站的另一个问题中看到):

<?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;
但我不明白如何将上面的代码翻译成适合我当前功能的代码。我用它来获取帖子视图:

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);
    }
}
我希望这是有意义的,并提前表示感谢。

1 个回复
最合适的回答,由SO网友:Bainternet 整理而成

您可以使用当前拥有的内容并添加新条件:

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;
        }
}

结束