Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WP查询的排序结果不适用于orderby和order参数_Php_Wordpress_Taxonomy - Fatal编程技术网

Php WP查询的排序结果不适用于orderby和order参数

Php WP查询的排序结果不适用于orderby和order参数,php,wordpress,taxonomy,Php,Wordpress,Taxonomy,我的目标是按帖子的名称(存储为($category[0]->cat_name))排序,而不是按日期。我理解,默认情况下,wp_查询按日期对其进行排序,这就是为什么它们当前按日期排序,如下所述: 我的代码如下: $args = array( 'post_type' => 'pastpapers', 'posts_per_page' => 20000, // the value of orderby doesn'

我的目标是按帖子的名称(存储为($category[0]->cat_name))排序,而不是按日期。我理解,默认情况下,wp_查询按日期对其进行排序,这就是为什么它们当前按日期排序,如下所述:

我的代码如下:

$args = array(
            'post_type' => 'pastpapers', 
            'posts_per_page' => 20000, 
            // the value of orderby doesn't change anything
            'orderby' => 'date',
            // the value of order doesn't change anything
            'order'   => 'DESC',
            'post_status' => 'publish',
            'tax_query' => array(
                 array(
                     'taxonomy' => 'category',
                     'field' => 'term_id',
                     'terms'    => $category->term_id
                 ),
            ),
        );
$index = 0;
        
        $dataQuery = new WP_Query($args);
        if ($dataQuery->have_posts()) :
            while ($dataQuery->have_posts()) : $dataQuery->the_post();
                $category = get_the_category($post->ID);
                
                $tempLink = get_permalink();
                $tempDate = date('Y', strtotime($post->post_date));
                $tempTitle = get_the_title();
                $tempName = $category[0]->cat_name;
        
                $testArray2 = array(
                    'link' => $tempLink,
                    'date' => $tempDate,
                    'title' => $tempTitle,
                    'name' => $tempName
                );
                
                $testArray[$index++] = $testArray2;
                
            
            endwhile;
        endif;
        wp_reset_postdata();
我不认为我的循环有问题,但下面是代码:

$dataQuery = new WP_Query($args);
        if ($dataQuery->have_posts()) : 
            while ($dataQuery->have_posts()) : $dataQuery->the_post();
        
                $category = get_the_category($post->ID);
                
                echo '<strong class="d-block text-secondary2 text-uppercase">'. $category[0]->cat_name .'</strong>';

            endwhile; 
        endif; 
        

        // Restore original Post Data
        wp_reset_postdata();

    } // End foreach
    ?>
$dataQuery=新的WP\u查询($args);
如果($dataQuery->have_posts()):
而($dataQuery->have_posts()):$dataQuery->the_post();
$category=获取\u类别($post->ID);
echo“”$类别[0]->类别名称。;
结束时;
endif;
//恢复原始Post数据
wp_reset_postdata();
}//结束foreach
?>
我尝试过改变orderby和order的值,并完全删除它们,但网站上帖子的顺序没有改变。我假设这是因为它使用的是默认顺序,而不是我在orderby和order的参数中输入的任何值。但是我不知道为什么它使用默认值而不是我的自定义值

我读过类似的问题:

但我仍然不知道如何解决我的问题。 我还阅读了一个关于堆栈溢出的类似问题,其中似乎是一个插件覆盖查询的问题,但我不认为是这样,因为我没有使用该插件


任何帮助都将不胜感激。

我找到了一个可能更复杂一点的解决方案,但至少它解决了我的问题

所以我试图让WP_查询按标题排序,它按默认值排序我的帖子,默认值是date,而不是我想要的

因此,我得到了与我的查询匹配的所有帖子,将它们放入一个数组中,如下所示:

$args = array(
            'post_type' => 'pastpapers', 
            'posts_per_page' => 20000, 
            // the value of orderby doesn't change anything
            'orderby' => 'date',
            // the value of order doesn't change anything
            'order'   => 'DESC',
            'post_status' => 'publish',
            'tax_query' => array(
                 array(
                     'taxonomy' => 'category',
                     'field' => 'term_id',
                     'terms'    => $category->term_id
                 ),
            ),
        );
$index = 0;
        
        $dataQuery = new WP_Query($args);
        if ($dataQuery->have_posts()) :
            while ($dataQuery->have_posts()) : $dataQuery->the_post();
                $category = get_the_category($post->ID);
                
                $tempLink = get_permalink();
                $tempDate = date('Y', strtotime($post->post_date));
                $tempTitle = get_the_title();
                $tempName = $category[0]->cat_name;
        
                $testArray2 = array(
                    'link' => $tempLink,
                    'date' => $tempDate,
                    'title' => $tempTitle,
                    'name' => $tempName
                );
                
                $testArray[$index++] = $testArray2;
                
            
            endwhile;
        endif;
        wp_reset_postdata();
一旦我在我的数组中得到了我想要的所有帖子,我就开始按照我想要的顺序对它们进行排序,这很复杂,因为我发现一些标题以1到20之间的数字开头,而其他帖子的标题没有数字。起初我只是使用strcmp()函数,但这不起作用,因为数字在字母之前,我希望它们在字母之后,例如: 代数,函数,4。分化,10。因式分解

并不是这样分类的: 4.分化,10。因式分解、代数、函数

因此,要做到这一点,我必须首先获取标题中的任何数字,如果有,我使用了preg\u match\u all()函数,然后使用infrade()函数将数组转换为变量

然后我检查是否不等于0,这意味着字符串中有一个数字,而不仅仅是一个没有数字的字符串

我一次只检查一个,并将I与I+1进行比较,从而对它们进行排序。我知道这不是最有效的排序方法,但它是有效的

我创建了一个变量$sorts,将其设置为数组的大小,并每次递减以确保整个数组已排序。不是很好的编码,但它可以工作

下面是执行此操作的代码:

$sorts = sizeof($testArray);
        while($sorts > 0) {
            for($i = 0; $i + 1 < sizeof($testArray); $i++) {
                preg_match_all('!\d+!', $testArray[$i]['name'], $matches);
                preg_match_all('!\d+!', $testArray[$i+1]['name'], $matches2);
                $var1 = implode(' ', $matches[0]);
                $var2 = implode(' ', $matches2[0]);

                if($var1 != 0 && $var2 != 0) {
                    if($var1 > $var2) {
                        $temp = $testArray[$i];
                        $testArray[$i] = $testArray[$i+1];
                        $testArray[$i+1] = $temp;
                    }
                }
                else if(($var1 != 0 && $var2 == 0)) {
                    $temp = $testArray[$i];
                    $testArray[$i] = $testArray[$i+1];
                    $testArray[$i+1] = $temp;
                }
                else if(($var1 == 0 && $var2 != 0)) {
                    //do nothing
                }
                else if(strcmp($testArray[$i]['name'], $testArray[$i+1]['name']) > 0) {
                    $temp = $testArray[$i];
                    $testArray[$i] = $testArray[$i+1];
                    $testArray[$i+1] = $temp;
                }
            }
            $sorts--;
        }
$sorts=sizeof($testArray);
而($sorts>0){
对于($i=0;$i+1$var2){
$temp=$testArray[$i];
$testArray[$i]=$testArray[$i+1];
$testArray[$i+1]=$temp;
}
}
else if(($var1!=0&&$var2==0)){
$temp=$testArray[$i];
$testArray[$i]=$testArray[$i+1];
$testArray[$i+1]=$temp;
}
else if(($var1==0&&$var2!=0)){
//无所事事
}
else if(strcmp($testArray[$i]['name'],$testArray[$i+1]['name'])>0){
$temp=$testArray[$i];
$testArray[$i]=$testArray[$i+1];
$testArray[$i+1]=$temp;
}
}
$sorts--;
}
一旦我得到了我想要的排序后的帖子,我所需要做的就是一个简单的for循环,然后像我想要的那样回显帖子:

for($i = 0; $i < sizeof($testArray); $i++) {
  //echo code
}
for($i=0;$i

这就行了。

不幸的是,你不能像这样在
WP\u查询中按类别排序,因为一篇文章可以有很多类别,所以没有明确的逻辑来决定如何对文章排序,因为它们有多个值,这些值在顺序中可能有不同的位置。根据您想做什么,您可以尝试不同的选项,例如,对不同类别进行多个查询。如果你让我们知道你想要做什么,我们可能会帮助你想出一个替代的解决方案。非常感谢@FluffyKitten我找到了一个有效的选项,并将其发布在下面。我感谢你的帮助。