i want order by meta value pro first then meta value free in my search function
-
my meta key is geo_listing_type and this key have 2 meta_value pro and free i want it order by pro first then free with random. below is my function.!
function gc_multi_search( $sfrom, $location, $limit = null ) {
global $wpdb;
if ( !empty( $sfrom ) || !empty( $location ) )
$n = ‘%’;
$post_type = POST_TYPE;
$post_status = ‘publish’;
$meta_key = “geo_address”;
$query = ”;
if ( $sfrom !== ” && $location == ” ) {
$query = “SELECT $wpdb->posts.*
FROM
$wpdb->posts
INNER JOIN $wpdb->term_relationships
ON $wpdb->term_relationships.object_id = $wpdb->posts.ID
INNER JOIN $wpdb->term_taxonomy
ON $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id
INNER JOIN $wpdb->terms
ON $wpdb->terms.term_id = $wpdb->term_taxonomy.term_id
WHERE post_status = ‘$post_status’ AND post_type = ‘$post_type’ AND
(post_title LIKE ‘{$n}$sfrom{$n}’ OR post_content LIKE ‘{$n}$sfrom{$n}’ OR $wpdb->terms.name = ‘$sfrom’) GROUP BY $wpdb->posts.ID {$limit}”;
} elseif ( $sfrom == ” && $location !== ” ) {
$query = “SELECT *
FROM
$wpdb->posts
INNER JOIN $wpdb->postmeta
ON $wpdb->posts.ID = $wpdb->postmeta.post_id
WHERE post_status = ‘$post_status’ AND post_type = ‘$post_type’ AND
(meta_key = ‘$meta_key’ AND meta_value like ‘{$n}$location{$n}’)
GROUP BY $wpdb->posts.ID {$limit}”;
} elseif ( $sfrom !== ” && $location !== ” ) {
$query = “SELECT $wpdb->posts.*
FROM
$wpdb->posts
INNER JOIN $wpdb->postmeta
ON $wpdb->posts.ID = $wpdb->postmeta.post_id
INNER JOIN $wpdb->term_relationships
ON $wpdb->term_relationships.object_id = $wpdb->posts.ID
INNER JOIN $wpdb->term_taxonomy
ON $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id
INNER JOIN $wpdb->terms
ON $wpdb->terms.term_id = $wpdb->term_taxonomy.term_id
WHERE post_status = ‘$post_status’ AND post_type = ‘$post_type’ AND
(post_title like ‘{$n}$sfrom{$n}’ OR post_content like ‘{$n}$sfrom{$n}’ OR $wpdb->terms.name = ‘$sfrom’) AND
(meta_key = ‘$meta_key’ AND meta_value like ‘{$n}$location{$n}’)
GROUP BY $wpdb->posts.ID {$limit}”;
}
$results = array();
$results[‘result’] = $wpdb->get_results( $query );
$results[‘query’] = $wpdb->query( $query );
return $results;
}above is my search function.
my post type is = listing meta key = geocraft_listing_type meta_value pro and free so i want this above search function as pro value first then free with random.
and right now as per function it came with id. and if i add after group by ASC and DESC then it will work .but i want pro value first then free.. thanks. ??
waiting for your reply thanks ??
- The topic ‘i want order by meta value pro first then meta value free in my search function’ is closed to new replies.