Support

Account

Home Forums General Issues Getting merged results Reply To: Getting merged results

  • Progress.

    With this code in functions.php I can get the AND posts; that is, posts tagged with “Toronto” AND posts that have “Toronto” assigned to the meta_city field.

    I need the “OR” of course. Still searching. All feedback is welcome.

    
    add_action( 'pre_get_posts', 'md_modify_tag_archive_to_include_acf_fields' );
    
    function md_modify_tag_archive_to_include_acf_fields( $query ) {
    	if( $query->is_main_query() && $query->is_tag ) {
    		$term_name = $query->query[tag];
    		$term = get_term_by('name',$term_name, 'post_tag');
    
    		$meta_query = array(
    			'relation' => 'OR',
    			array(
    				'key' => 'meta_city', // name of custom field
    				'value' => $term->term_id, 
    				'compare' => 'LIKE'
    			)
    		);
    		$query->set( 'meta_query', $meta_query);
    	}
    
    }