Support

Account

Home Forums General Issues Getting merged results

Solving

Getting merged results

  • The backstory:

    I have created and ACF section that lists all tags in a drop down, and allows the user to select one. So for example, the existing tag “Toronto” can be assigned to the meta_city field. Once done, the user un-tags “Toronto” from the post. This is all fine.

    On the tag archive page though, nothing shows up because the post is no longer tagged with “Toronto,” it is assigned to the post via the meta_city field. So after reading the docs, I created the code to create a query object that returns all posts that have had “Toronto” (or whatever) assigned to the meta_city field, like this:

    
    $entries = get_posts(array(
    	'post_type' => 'post',
    	'meta_query' => array(
    		array(
    			'key' => 'meta_venue', // name of custom field
    			'value' => $term_id,
    			'compare' => 'LIKE'
    		)
    	)
    ));
    

    This also works fine.

    Now, the problem.

    I need to account for both posts tagged with “Toronto” AND posts that have “Toronto” assigned to the meta_city field, because “Toronto” can also be used as a simple tag on a post.

    So my question is: how can I insert this query into the main query so that I get posts tagged and post assigned in the same result set with no duplicates?

  • 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);
    	}
    
    }
    
  • Hi @the_MikeD

    The most effective solution is to use tags as they are supposed to be used.
    Instead of all this meta_query and merge logcic, which will drasticaly slow down your website, why not just use the taxonomy to filter the posts as it was created to do?

    When you edit the field group, you will see a setting on your taxonomy field that allows ‘load and save taxonomy term connection’. This will create the link between the taxonomy term object, and the post so you can perform normal a WP friendly tax_query.

    Thanks
    E

  • The reason why I can’t do as you suggest is because enabling that option severs the link between the ACF taxonomy field and the data it’s trying to store.

    From what I’ve seen in ACF5, when this option is enabled and I select “Toronto” from the “Born” dropdown, two things happen. First, “Toronto” is assigned to the post as a tag (or in my case as a custom taxonomy term) and second, the “Born” dropdown reverts to default (meaning “Toronto” no longer appears in the “born” dropdown).

    So no, this isn’t a realistic solution, which is why I’m doing what I’m doing.

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

The topic ‘Getting merged results’ is closed to new replies.