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 wh
I found a solution that might be a bit more complicated but at least it solves my problem.
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();
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
and not sorted like this: 4. Differentiation, 10. Factorization, Algebra, Functions
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
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 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.
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--;
}
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
}
And there we go that works.