Support

Account

Home Forums General Issues Order archive based on custom post object metakey value

Solving

Order archive based on custom post object metakey value

  • Hi all
    Hope someone can help me with this.

    I have two custom post types: one called Artwork and one called Artist.

    Artwork has a custom post_object field in which the user selects the Artist that created the artwork.

    The Artist custom post type has a custom ‘surname’ field.

    i.e.
    Custom post type 1 = Artist.
    Custom post type 2 = Artwork.
    Custom field on Artwork = post_object Artist
    Custom field on Artist = Surname

    Getting the artist name is easy once I’ve got the query results – get_field(‘surname’,’artist_id’) – but I want to be able to order the Artwork results by the Artist’s surname which means doing some kind of pre_get_posts thing on the query based on the post_object field id.

    I’m looking to use wp_query rather than a custom select query because I’m using hooks from an existin plugin.

    Any help or direction would be greatly appreciated!
    Thanks, Steve

  • Sorry to make the title so dull

  • Hi Steve,

    You can’t do what you’re asking just by modifying the wp_query with pre_get_posts.. Atleast not by querying artwork.

    However, you can instead of directly querying artwork and trying to order those query the artists.
    So you would do a custom wp_query for all artists and order them by their lastname like so:

    
    $args = array(
    'post_type' => 'artist',
    'orderby' => 'meta_value',
    'meta_key' => 'surname',
    'posts_per_page' => -1
    );
    

    Then as you loop through each artist you query their artwork (inside the artist loop).

    
    $artist_ID = get_the_ID();
    $artwork_args = array(
    	'post_type' => 'artwork',
    	'posts_per_page' => -1,
    	'meta_query' => array(
    		array(
    			'key' => 'artist_dropdown',
    			'value' => $artist_ID,
    			'compare' => '='
    		)
    	)
    );
    
  • Thanks Jonathan. Will try this in the next week and mark as Solved or write some feedback

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Order archive based on custom post object metakey value’ is closed to new replies.