Get the solution ↓↓↓
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.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.