Viewing 1 replies (of 1 total)
  • Thread Starter Paul Biron

    (@pbiron)

    I found the solution to my problem: using the posts_join and posts_where filters. The following is a simplified version of what I’ve added to my theme’s functions.php:

    function
    join_usermeta ($join)
    {
    	global $wp_query, $wpdb ;
    
    	if (!empty($wp_query->query_vars['s'])) {
    		$join .= " JOIN $wpdb->users ON $wpdb->posts.post_author = $wpdb->users.ID JOIN $wpdb->usermeta ON $wpdb->users.ID = $wpdb->usermeta.user_id JOIN $wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id" ;
    		}
    
    	return $join ;
    }
    add_filter ('posts_join', 'join_usermeta') ;
    
    function
    search_usermeta ($where)
    {
    	global $wp_query, $wpdb ;
    
    	if (!empty($wp_query->query_vars['s'])) {
    		$where .= " OR ($wpdb->usermeta.meta_key = 'description' AND $wpdb->usermeta.meta_value LIKE '%" . $wp_query->query_vars['s'] . "%' AND $wpdb->posts.post_author = $wpdb->users.ID AND $wpdb->postmeta.meta_key = '_wp_page_template' AND $wpdb->postmeta.meta_value = 'memberArtist.php' AND $wpdb->posts.post_status = 'publish')" ;
    		}
    
    	return $where ;
    }
    add_filter ('posts_where', 'search_usermeta') ;
    
    function
    search_distinct ()
    {
    	global $wp_query ;
    
    	if (!empty($wp_query->query_vars['s'])) {
    		return "DISTINCT" ;
    		}
    
    	return '' ;
    }
    add_filter ('posts_distinct', 'search_distinct') ;

    Note that the posts_distinct filter isn’t necessary in this simplified version, but the actual joins I do in the full version require it.

    I also had to add a get_the_excerpt filter (again, this is a simplified version of the filter I wrote):

    function dynamic_excerpt ($output) {
    	global $post ;
    
    	if (!(has_excerpt () && !is_attachment ())) {
    		$page_template = get_post_meta ($post->ID, '_wp_page_template', true) ;
    
    		if ($page_template == 'memberArtist.php') {
    			$output = get_post_meta ($post->ID, 'description', true) ;
    		}
    
    	return $output ;
    }
    add_filter ('get_the_excerpt', 'dynamic_excerpt') ;
Viewing 1 replies (of 1 total)
  • The topic ‘searching user metadata’ is closed to new replies.