Support

Account

Home Forums Feature Requests Relationship Field: filter for posts (i.e. image)

Solved

Relationship Field: filter for posts (i.e. image)

  • Would be nice to have the relationship field (select) only display posts which has an image (or another field, based on some rules set in acf). at the moment i have a very long list. i set to show only a specific cpt and taxonomy but i still have some posts, which are incomplete (image missing). they should appear in the select field.

  • Hi @nicmare,

    This is something you can hack using the acf/fields/relationship/query filter. This filter will allow you to modify the $args array used to query the posts. For instance, should you want to query only posts which have featured image set, then you could use the following code:

    add_filter('acf/fields/relationship/query', 'my_relationship_query', 10, 3);
    
    function my_relationship_query( $args, $field, $post ){
        
        $args['meta_query'] = array(
    
        	'key' => '_thumbnail_id',
            'compare' => 'EXISTS'
        );
    
        return $args;
    }

    For more info on this, have a look at

  • nice one but this is the correct query:

    // only show results with image in relationship field
    add_filter('acf/fields/relationship/query', 'my_relationship_query', 10, 3);
    function my_relationship_query( $args, $field, $post ){
        
        $args['meta_query'] = array(
    	    array(
    	    	// just check if this key exists:
    	        'key' => '_thumbnail_id'
    	    )
    	);
    
        return $args;
    }

    thank you for leading me into the right direction!

  • Thanks for the share.

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

The topic ‘Relationship Field: filter for posts (i.e. image)’ is closed to new replies.