Support

Account

Home Forums Front-end Issues Orderby Title is ordering by related field…???

Solved

Orderby Title is ordering by related field…???

  • This is a weird one. I’m querying a custom post type and wanting to order the results by title. Nothing special other than I do I have a meta query to filter by a custom taxonomy and a custom field.

    But for some reason, when orderby=title, it is rendering the posts by the title of a RELATION field that I’m pulling in the middle of this query. I’m wondering if it has something to do with either the meta queries or the $post variable. Here’s the code (with a lot of the un

    
    $args = array(
    	'post_type' => 'cpt-stouts',
    	'order' => 'ASC',
    	'orderby' => 'title',
    	'posts_per_page' => -1,
    	'ct-stout-years' => '2015',
    	'meta_query' => array(
    		array(
    			'key' => 'cf-stout-type',
    			'value' => 'guest',
    			'compare' => 'LIKE'
    		)
    	)
    );
    $temp = $wp_query;
    $wp_query = null;
    $wp_query = new WP_Query($args);
    if ($wp_query->have_posts()) :
    	while ($wp_query->have_posts()) : $wp_query->the_post();
    		setup_postdata($post);
    		$post_id = get_the_ID();
    ?>
    <h3 class="stout-title"><?php the_title(); ?></h3>
    	<?php
    		$abv = get_field('cf-stout-abv', $post_id);
    		$breweries = get_field('cf-stout-brewery', $post_id);
    		if( $breweries ):
    			foreach( $breweries as $post): 
    				setup_postdata($post); 
    				$title = get_the_title($post); echo $title;
    			endforeach;
    			wp_reset_postdata(); 
    		endif; 
    	endwhile; 
    endif;
    $wp_query = null;
    $wp_query = $temp;
    wp_reset_query();
    

    So for example that renders:

    Some Beer
    Brewed by ACF

    Another Beer
    Brewed by BCF

    Yet Another Beer
    Brewed by CCF

    They are being ordered by RELATED field (not the field in the query either) instead of the title for some reason. Help?

  • And of course I just figured it out–for some reason the setup_postdata($post) in the first query was throwing it off. Leaving this here in case someone else runs into it.

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

You must be logged in to reply to this topic.