• Hello I’m trying to display my posts based on a custom field value first and if my posts doesn’t have any custom field display them after.
    ex :
    first display the posts witch have a custum field value (meta_value_num ASC)
    post1 > key = 1
    post2 > key = 2
    post3 > key = 3
    after display all the other width custom field empty (alphabetical ASC)
    post5 > key = empty
    post6 > key = empty
    post7 > key = empty

    $args = array (
    	'post_type' => 'sellers',
    	'tax_query' => array(
    		array(
    			'taxonomy' 	=> 'city',
    			'field' 	=> 'slug',
    			'terms' 	=> $term_slug,
    		)
    	),
    	'meta_query'=> array(
    		'relation' => 'OR',
    		array( //if key exclusive is not empty
    			'key' => 'exclusive',
    			'value' => '1',
    			'compare' => '>=',
    			'meta_type'      => 'NUMERIC',
    		),
    		array( //if key exclusive is empty
    			'key' => 'exclusive',
    			'value' => '1',
    			'compare' => 'NOT EXISTS'
    		)
    	),
    	'meta_key' => 'exclusive',
    	'orderby'    => 'meta_value_num title',
    	'order'      => 'ASC DESC',
    );

    Can you help me please ?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter grumo64

    (@grumo64)

    Anyone could help me ?

    Thread Starter grumo64

    (@grumo64)

    I’m still trying to find so if I try :

    $args = array (
    	'post_type' 	=> 'sellers',
    	'tax_query' 	=> array(
    		array(
    			'taxonomy' 	=> 'city',
    			'field' 	=> 'slug',
    			'terms' 	=> $term_slug,
    		)
    	),
    	'meta_key' 		=> 'exclusive',
    	'orderby'  		=> 'meta_value_num',
    	'order'    		=> 'DESC',
    	'meta_query'=> array(
    		'relation' 		=> 'OR',
    		array(
    			'key' 		=> 'exclusive',
    			'value' 	=> '',
    			'compare' 	=> 'NOT EXISTS',
    			'type'		=> 'NUMERIC'
    		)
    	),
    );

    everything is ok but not in good order (due to DESC)
    The result is:
    post3 > key = 3
    post2 > key = 2
    post1 > key = 1
    and all other posts orderby by title DESC

    but if I turn the order parameter to ASC the result is:
    post1 > key = 1
    post5 > key = empty
    post6 > key = empty
    post2 > key = 2
    post3 > key = 3

    what’s wrong ?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Order post witch have custom fields first and all the other after’ is closed to new replies.