Support

Account

Home Forums Backend Issues (wp-admin) Search relationship on custom field

Solving

Search relationship on custom field

  • When I add this code, the search stops working all together…

    
    function modify_acf_relationship_search_query( $args, $field, $post_id ) {
        // Be very selective when running code at the pre_get_posts hook
        if ( ! is_admin() || empty( $args['s'] )) {
            return $args;
        }
        
    	
    	$args['meta_query'] = array(
    		'relation' => 'OR', // Optional, defaults to "AND"
    		array(
    			 'key' => '_sku',
    			 'value' => '"%' . $args['s'] .'%"',
    			 'compare' => 'LIKE',
    		));
    
        return $args;
    }
    add_filter('acf/fields/relationship/query', 'modify_acf_relationship_search_query', 10, 3);
    
  • I’m assuming that you’re getting no results.

    The reason is that even though you are putting an OR in your meta query it doesn’t have any effect because when WP runs the query it is looking for the search value in the title AND in the post meta value.

    I believe this question has been asked before. As far as I know there isn’t any way in WP to do a query where something can appear in the title or in a post meta field. You’ll either end up getting everything or nothing.

    The only way to successfully do a search like this it to write your own SQL queries using $wpdb. It’s a limitation in WP_Query.

  • I’m looking to do something similar, but I’m focusing on the use case that the user will enter either a product name or a SKU. Name is the title field, so that will come back in results by default. If they enter a SKU, that’s an ACF field.

    So, what I’m doing is firing a query against the default $args array, and if it contains posts, I return that array as-is.

    If it doesn’t return any posts, then I overwrite $args with a new array that looks at my custom field, and also removes the ‘s’ argument to kill the default search functionality.

    It seems to be working pretty well given this use case. I tried to combine both conditions using post__in, but you still wind up with the situation where you have the ‘s’ param searching in title and content, and creating a default ‘AND’ relationship with the meta_query. Also, if the title/content search succeeds, it never looks at the SKU, but that’s not an issue in my case since the SKU value is somewhat human un-readable and would make for a pretty lousy product title.

    My code is below for reference:

    
    function my_related_query($args, $field, $post_id) {
    	
    	if ($args['s'] == '') {
    		// nothing passed in, so just return $args as it stands and get out of here.
    		return $args;
    	}
    	
    	// check for posts using $args
    	$result = new WP_Query($args);
    	if ($result->found_posts == 0) {
    		
    		// no posts found for the query, so it might be a sku... take a look there?
    		$args['meta_query'] = array(
    			array(
    				'key' => 'sku',
    				'value' => $args['s'],
    				'compare' => 'like'
    			)
    		);
    		$args['posts_per_page'] = -1;
    		$args['s'] = '';
    		
    	}
    	
    	return $args;
    	
    	
    }
    add_filter('acf/fields/relationship/query/name=related', 'my_related_query', 10, 3);
    
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Search relationship on custom field’ is closed to new replies.