query doesnt sort results


#1

[php]<?php

			global $wpdb, $post;
			$results = $wpdb->get_results(
					"
					SELECT * FROM $wpdb->posts
					WHERE post_title LIKE '$mo%'
					AND post_type = 'movies'
					AND post_status = 'publish';
					"
			 );
		
		sort( $results );

			 if ( $results ) {

				foreach ( $results as $post ) {

					setup_postdata ( $post ); 

		?> [/php]

I have no Idea why this doesn’t sort my results out alphabetically.

any help appreciated


#2

sort doesn’t know how it should sort the results, if you look at what data is in results then how on earth should the sort function know what you want to sort by?

Add a sort (order by) condition to your query. Much faster to sort in sql when getting the results anyway


#3

I have tried it your way also but I am obviously doing it wrong as I get no results.


#4

What did you try?


#5

SORT BY post_title DESC


#6

Its ORDER BY.


#7

[php]<?php

			global $wpdb, $post;
			$results = $wpdb->get_results(
					"
					SELECT * FROM $wpdb->posts
					WHERE post_title LIKE '$mo%'
					AND post_type = 'movies'
					AND post_status = 'publish'
					ORDER BY post_title ASC;
					"
			 );
		
		sort( $results );

			 if ( $results ) {

				foreach ( $results as $post ) {

					setup_postdata ( $post ); 

		?> [/php]

so this will work ? right ?


#8

Yes that should work fine.