• I’m relatively new to WP and have spent the last several hours searching the docs and haven’t found an answer, so if the answer is in the docs somewhere, please just refer me to the proper place.

    I have created a page template that populates pages with information from the page author’s user metadata. However, the default search mechanism isn’t finding any of these pages when a search is performed using terms contain in a user’s metadata. How can I get the search results to include these pages whose content is dynamically generated from user metadata?

    To be more concrete, a simplified version of the page template looks like:

    <div class='username'><?php the_author_meta ('display_name')</div>
    <div class='bio'><?php the_author_meta ('description')</div>

    And suppose there is a user ‘jdoe’ who is the author of a page and has the term ‘photography’ is the ‘Biographical Info’ (description) field in his user profile.

    Searching for ‘photography’ currently finds all posts and static pages that contain that term, but does NOT find the page owned by user jdoe, whose content is dynamically generated from the ‘description’ field in jdoe’s user profile, even tho it contains that term.

    Similarly, I have another page template that includes

    <?php wp_list_bookmarks('title_li=&category_before=&category_after='); ?>

    but searches for terms contained in the ‘Name’ field of Links do NOT find the page that users that page template.

    So, my question is: how to I augment the search results to include pages that contain dynamically generated content?

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.