Support

Account

Home Forums ACF PRO Filtering by relationship field in pre_get_posts on post archive page

Solving

Filtering by relationship field in pre_get_posts on post archive page

  • Hello –

    I have a custom post type, “industries”, that news posts can select from in a Relationship field called “related_industries”. I have no problem getting a list of related industries on the post page, or anywhere that related_industries exists, or getting the list of posts on the single-industry page with the reverse lookup. But I’d like to be able to filter the results on the post archive page by industry, using a query var. It seems in theory that I should be able to do it the same way as I would on a single-industry page, but it doesn’t seem to affect the query at all. I’ve tried a lot of different variations, too many to list, but I’m starting to wonder if it’s possible (especially since I haven’t seen too many people asking the same question).

    This is basically the code – a few things have been simplified posting it here (because I’m trying to do this with multiple CPTs) but this should give you the gist of it. Any idea what I might be missing? Thanks!

    
    function query_by_relationship( $query ) {
    	
    	if( is_admin() ) {
    		return $query;
    	}
    	if (is_archive()) {	
    		if( isset($_GET['related_industries']) ) {
    			$meta_query[] = array(
    							array(
    								'key' 		=> 'related_industries',
    								'value' => '"' . $_GET['related_industries'] . '"',
    								'compare' 	=> 'LIKE',
    							)
    						);
    			$query->set('meta_query', $meta_query);
        		} 
    		
    	}
    	
    	
    	// return
    	return $query;
    
    }
    
    add_action('pre_get_posts', 'query_by_relationship');
    
  • There is an issue here

    
    $meta_query[] = ...
    

    should be

    
    $meta_query =
    

    If that does not help, what is in $_GET['related_industries'], does this only have a single selection or can multiple selections be made?

  • Well, this is awkward. I had taken that code out of functions.php to try things another way. I pasted back in my code from above, tweaked a few var names as needed, and it worked perfectly fine (whether or not I had the [] in, tried it both ways). So whatever the problem in the code was, it must have been one of the extra bits that I had in there before stripping it down to post on the forum!

    Thanks anyway for your response, apparently I just needed a sounding board 😉

  • meta query allows nested queries, do something like

    
    $meta_query = array(
      array(
        array(
          // actual query here
        )
      )
    )
    

    will work, however, this can cause the query to run slower and you’re better off without the extra nesting if you don’t need it.

  • Hmmm, spoke too soon. My code now works fine on post archive pages, but I need to add it to the CPT “events” as well, which uses the exact same set of custom fields. But this isn’t working. I can test and see that it is correctly triggering the various if statements, but it doesn’t seem that the meta query gets applied – it just returns all results, unfiltered. Posts are still working, though, with the exact code below. Does this sort of meta query get applied differently to a CPT than to the standard post?

    function query_by_relationship( $query ) {
        
        if( is_admin() ) {
            return $query;
        }
    
        if ((is_archive() && $query->is_main_query()) || ( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'events' ) ) { 
            
            if( isset($_GET['sector']) ) {
                $meta_query = array(
                                array(
                                    'key'       => 'related_industries',
                                    'value' => '"' . $_GET['sector'] . '"',
                                    'compare'   => 'LIKE',
                                )
                            );
                $query->set('meta_query', $meta_query);
            } else if( isset($_GET['pa']) ) {
                $meta_query = array(
                                array(
                                    'key'       => 'related_practice_areas',
                                    'value' => '"' . $_GET['pa'] . '"',
                                    'compare'   => 'LIKE',
                                )
                            );
                $query->set('meta_query', $meta_query);
            } else if( isset($_GET['lawyer']) ) {
                $meta_query = array(
                                array(
                                    'key'       => 'related_people',
                                    'value' => '"' . $_GET['lawyer'] . '"',
                                    'compare'   => 'LIKE',
                                )
                            );
                $query->set('meta_query', $meta_query);
            }
            
        }
        
        
        // return
        return $query;
    
    }
    
    add_action('pre_get_posts', 'query_by_relationship');
    
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Filtering by relationship field in pre_get_posts on post archive page’ is closed to new replies.