WP Query 的排序结果不适用于 orderby 和 order 参数 [英] Ordering results of WP Query not working for orderby and order parameters
问题描述
我的目标是按存储为 ($category[0]->cat_name) 而不是按日期的名称对帖子进行排序.我知道默认情况下 wp_query 按日期对它们进行排序,这就是为什么它们目前按此处所述的日期排序:订单参数
My goal is to order posts by their name which is stored as ($category[0]->cat_name) and not by date. I understand that by default the wp_query orders them by date which is why they are currently ordered by date as stated here: Order Parameters
我的代码如下:
$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
),
),
);
我不认为问题出在我的循环上,但无论如何这里是代码:
I don't think the problem is with my loop but here is the code anyways:
$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
?>
我尝试更改 orderby 和 order 的值,并完全删除它们,但网站上帖子的顺序没有改变.我认为这是因为它使用的是默认顺序,而不是我在 orderby 和 order 的参数中放入的任何值.但是我不知道为什么它使用默认值而不是我的自定义值.
I have tried changing the values of orderby and order, and completely removing them but the order of the posts on the website doesn't change. I assume that it is because it is using the default order and not whatever values I put in the arguments for orderby and order. However I don't know why it is using the default values and not my custom ones.
我读过这个类似的问题:orderby not working
I have read this similar question: orderby not working
但仍然不知道如何解决我的问题.我还阅读了有关堆栈溢出的类似问题,其中似乎是插件覆盖查询的问题,但我认为情况并非如此,因为我没有使用该插件.
But still don't know how to solve my issue. I also read a similar question on stack overflow where it seemed to be an issue with a plugin overwriting the query but I don't think that is the case as I am not using that plugin.
任何帮助将不胜感激.
推荐答案
我找到了一个可能有点复杂但至少解决了我的问题的解决方案.
I found a solution that might be a bit more complicated but at least it solves my problem.
所以我试图让 WP_query 按标题排序,它按默认值排序我的帖子,这是日期而不是我想要的.
So I was trying to get the WP_query to order by title and it was ordering my posts by default value which is date and not what I wanted.
因此,我获取了与我的查询匹配的所有帖子,将它们放入一个数组中,如下所示:
So instead I got all the posts that matched my query, put them into an array as follows:
$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. 分解
Once I had all the posts I wanted in my array as I wanted them I then proceeded to sort them in the order that I wanted them which was complicated as I some titles started with numbers between 1 and 20 and other posts just had titles with no numbers. At first I just used the function strcmp() but that didn't work as the numbers would be before the letters and I wanted them to be after the letters eg: Algebra, Functions, 4. Differentiation, 10. Factorization
而不是这样排序:4. 微分,10. 因式分解,代数,函数
and not sorted like this: 4. Differentiation, 10. Factorization, Algebra, Functions
为此,我必须首先获取标题中的任何数字,如果有任何数字我使用了preg_match_all() 函数,然后将数组转换为我使用 preg_match_all() 函数的变量strong>implode() 函数
So to do that I had to first get any numbers in the title if there were any for which I used the preg_match_all() function and then to convert the array to a variable I used the implode() function
然后我检查是否有不等于 0 的值,这意味着字符串中有一个数字,而不仅仅是一个没有数字的字符串.
I then checked if there were not equal to 0 which would mean that it's there is a number in the string and not just a string without a number.
我通过一次只检查一个并将 i 与 i+1 进行比较来对它们进行排序.我知道这不是最有效的排序方式,但确实有效.
I sorted them by checking only one at a time with comparing i to i+1. I know that it's not the most efficient way to sort it but it works.
我创建了一个变量 $sorts 并将其设置为数组的大小并每次递减它以确保整个数组已排序.不是很好的编码,但它有效.
I created a variable $sorts and set it to the size of the array and decremented it each time to make sure the whole array was sorted. Not great coding but it works.
以下是执行此操作的代码:
Here is the code to do that:
$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--;
}
一旦我按照我想要的方式整理了帖子,我需要做的就是一个简单的 for 循环并按照我想要的方式回显帖子:
Once I had the sorted posts like I wanted them all I needed to do was a simple for loop and echo the posts like I wanted:
for($i = 0; $i < sizeof($testArray); $i++) {
//echo code
}
然后就可以了.
这篇关于WP Query 的排序结果不适用于 orderby 和 order 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!