• hi guys,

    i’m trying to sort my posts in a custom post order.

    
    $partner = new WP_Query( array( 
    	'post_type' => 'partner', 
    	'posts_per_page' => -1,
    	'nopaging' => true,
    	'meta_query' => array($meta_query)
    	 ) ); 

    starts my query. all fine.

    i’m trying to sort my posts in a cusstom sort order.

    this works:

    global $query_string;
    query_posts( $query_string . '&order=ASC' );

    this doesn’t:

    global $query_string;
    query_posts( $query_string . '&order=ASC&orderby=name' );

    why?

    i also tried

    $args = array_merge( $partner->query_vars, array( 'order' => 'DESC','orderby' => 'name' ) );
    query_posts( $args )

    no effect either. args are merged correctly, but sort order doesn’t change.

    2nd question:

    i execute a second query that checks inside custom post type projects which projects th partner took place in:

    function solarhaus_verlinkung($partner_name){
    	$meta_query[]=array(
    		'relation' => 'OR',
    			array('key' => 'planung_gebaeude','value' => $titel_clean,'compare' => 'LIKE'),
    		);
    	$solarhaeuser = new WP_Query( array( 
    		'post_type' => 'solarhaus',
    		'posts_per_page' => -1,
    		'meta_query' => array($meta_query)
    	) ); 
    	$solarhaeuser->wp_reset_postdata();
    	return $solarhaeuser;	
    }

    this gives me the number of the partners projects.
    what would be the best way to first sort by date ASC then by number of projects? could i set up something like

    while ( $partner->have_posts() ) : $partner->get_the_post()['projects_number'] = "xo";

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    There is no “name” field in the posts table, its label is “post_name”. You must order by a proper field label. You shouldn’t use query_posts() to modify a new WP_Query, that’s not how it works. It actually throws out the WP_Query results and runs a new query. Just specify the proper WP_Query arguments to order as desired when you instantiate the WP_Query object.

    Similarly, there is no “projects_number” field in a post object. I’m assuming this is a meta field? If so, get the current post object’s ID and use that in get_post_meta(). If you need to qualify a query by a meta value, you can use a “meta_query” argument. You can also order queries by meta values.

Viewing 1 replies (of 1 total)
  • The topic ‘WP query sort order’ is closed to new replies.