Php 在wordpress中按字数对帖子排序

Php 在wordpress中按字数对帖子排序,php,wordpress,sorting,Php,Wordpress,Sorting,我需要得到每个帖子的字数,并按照这个顺序显示帖子。我需要这一点,以确定与最低词数的职位,并对他们的工作 到目前为止,我已经能够根据每篇文章的字数创建一个数组,并使用sort($wc,sort\u NUMERIC)对它们进行排序。除此之外,我无法按照此顺序显示帖子 $args = array('post_type' => 'post', 'posts_per_page' => 20, 'post_status' => 'publish'); $results = get_

我需要得到每个帖子的字数,并按照这个顺序显示帖子。我需要这一点,以确定与最低词数的职位,并对他们的工作

到目前为止,我已经能够根据每篇文章的字数创建一个数组,并使用
sort($wc,sort\u NUMERIC)
对它们进行排序。除此之外,我无法按照此顺序显示帖子

$args = array('post_type' => 'post', 'posts_per_page' => 20, 'post_status' => 'publish'); 
   $results = get_posts($args);
   $wc = array(); 
   //$p_id = array(); //how do i use this ?       
       foreach($results as $r){
       $wc[] = str_word_count( strip_tags( $r->post_content ));
       //$p_id[] = $->ID; //how do i use this ?
       }
   sort($wc, SORT_NUMERIC);
正如您所知,我知道另一种选择,您可以将这些值保存为自定义元字段,然后使用
'meta\u value\u num'
显示帖子,但我不允许采用这种方式。你知道怎么做吗?蒂亚


<>这是我一直在尝试的事情:但是它没有考虑<代码>帖子ID<代码>。我想它应该像一个关联数组或什么的

 foreach ($wc as $key => $val) {
            echo $key.' => ' .$val . '<br>';
            }


更新-它与公认答案中提供的解决方案配合使用。如果你能想出更好的解决方案,请写一个答案。

我认为$wc应该是一个带有“标题”和“字数”的多维数组,然后你可以使用array\u mulisort。 试着这样想:

foreach($results as $r){
  $wc[] = array('title' => $r->post_title, 'nb_words' => str_word_count( strip_tags( $r->post_content )));
}
foreach($wc as $key => $post) {
  $nb_words[$key] = $post['nb_words'];
  $title[$key] = $post['title'];
}
array_multisort($nb_words, SORT_ASC, $title, SORT_ASC, $wc);
更新-以下是使用数组\u多端口的一种更短更好的方法:

$args = array('post_type' => 'post', 'posts_per_page' => 20, 'post_status' => 'publish'); 
$results = get_posts($args);
$nb_words = array();
$title = array();
foreach($results as $r){
  $nb_words[] = str_word_count( strip_tags( $r->post_content ));
  $title[] = $r->post_title;
}
array_multisort($nb_words, SORT_ASC, SORT_NUMERIC, $title);
for ($i = 0; $i < sizeof($title); $i++) {
  echo $title[$i] . ' => ' . $nb_words[$i] . '<br>';
}
$args=array('post_type'=>'post','posts_per_page'=>20,'post_status'=>'publish');
$results=get_posts($args);
$nb_words=array();
$title=array();
foreach(结果为$r){
$nb_words[]=str_word_count(strip_标签($r->post_内容));
$title[]=$r->post_title;
}
数组\多排序($nb\ U单词,排序\ ASC,排序\数值,$title);
对于($i=0;$i。$nb_单词[$i]。
; }
不需要通过索引映射单词计数数组和帖子标题数组。您可以创建一个按post title索引的数组,将单词计数作为值,然后在维护索引的同时使用按其值对数组进行排序:

$args = array( 'post_type' => 'post', 'posts_per_page' => 20, 'post_status' => 'publish' );
$results = get_posts( $args );

$posts = array();

foreach ( $results as $result ) {
    $posts[ $result->post_title ] = str_word_count( strip_tags( $result->post_content ) );
}

asort( $posts, SORT_NUMERIC );

die( print_r( $posts ) );

如果你想按相反的顺序对数组进行排序,你只需要使用而不是
asort

你在$results和sorted$wc中都有post\u id吗?@Mihai我猜
post\u id
可以在
foreach
中使用$r->id访问。我也尝试过创建post\u id数组,但不知道如何使用它。我在注意,这两个数组都应该有一个唯一的POST标识,比如PySyid,然后不知何故,基于这个公共密钥,以有序数组加入或排序你的结果数组,可能有一个PHP函数来做,你的意思是它不考虑后置ID吗?你需要在输出中看到帖子ID吗?太棒了。如果我们只能用更新的代码更改订单ASC或DESC。谢谢。根据您的请求,我刚刚将SORT\u ASC参数添加到array\u multisort命令中。
$args = array( 'post_type' => 'post', 'posts_per_page' => 20, 'post_status' => 'publish' );
$results = get_posts( $args );

$posts = array();

foreach ( $results as $result ) {
    $posts[ $result->post_title ] = str_word_count( strip_tags( $result->post_content ) );
}

asort( $posts, SORT_NUMERIC );

die( print_r( $posts ) );