Forums Forums Search & Filter Pro Keyword search in titles only?

Viewing 7 posts - 1 through 7 (of 7 total)
  • Anonymous
    #265566

    I’ve added a Search field using this plugin on my WooCommerce site to allow users to do a keyword search, but it’s matching keywords in the product descriptions. I’d like to restrict it to the product titles only. Is that possible?

    Anonymous
    #265567

    For clarity, here’s my form:

    form

    Trevor
    #265580

    Our plugin does not directly allow you to do text searches of individual Post Meta (custom fields) and Taxonomy (Category, Tags and Taxonomies) data/terms. The Search & Filter Pro Text Search field uses the standard WordPress search, so looks only in the Post Title and Content. To help you control this better, on the form’s Advanced settings tab you will see 2 settings for Relevanssi.

    The documentation for this is here:

    https://www.designsandcode.com/documentation/search-filter-pro/3rd-party/relevanssi/

    You would need to install and activate the free Relevanssi plugin as well.

    Then set Relevanssi up and build its index (make sure it is indexing the desired post types, custom fields AND any taxonomy that you want to search).

    To restrict to Title only, please see this post:

    https://support.searchandfilter.com/forums/topic/search-and-filter-only-in-title/#post-93961

    Anonymous
    #276329

    Will a text search against post meta be available in a future release?

    Trevor
    #276331

    Please note that these support forums are now closed. If you require support, please raise a support ticket through Your Account.

    Post Meta (custom fields), as the previous post I made on this ticket says, can already be searched using the Relevanssi option.

    Anonymous
    #276839

    Here’s a function for functions.php that will enable searching by title only, in various post types, without installing any other plugins. Hope this helps!

    
    /**
    * Fix search results in admin to search by page title only
    * Modified from: https://developer.wordpress.org/reference/hooks/posts_search/
     *
     * @param String $search
     * @param Object $wp_query Object
     * @return void
     */
    function __search_by_title_only( $search, $wp_query ) {
    	global $wpdb;
    
    	$enabled_post_types = array(
    		'post-type-slug',
    		'another-post-type',
    	);
    	if ( empty( $wp_query->query_vars['post_type'] ) || ! in_array( $wp_query->query_vars['post_type'], $enabled_post_types ) ) {
    		return $search; // skip processing - not an enabled post type
    	}
    
    	$q = $wp_query->query_vars;
    	$n = ! empty( $q['exact'] ) ? '' : '%';
    
    	$search    =
    	$searchand = '';
    	if ( empty( $q['search_terms'] ) ) {
    		$q['search_terms'] = array();
    	}
    	foreach ( (array) $q['search_terms'] as $term ) {
    		$term      = esc_sql( $term );
    		$search   .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
    		$searchand = ' AND ';
    	}
    
    	if ( ! empty( $search ) ) {
    		$search = " AND ({$search}) ";
    		if ( ! is_user_logged_in() ) {
    			$search .= " AND ($wpdb->posts.post_password = '') ";
    		}
    	}
    
    	return $search;
    }
    add_filter( 'posts_search', '__search_by_title_only', 1000, 2 );
    
    
    Trevor
    #276841

    Thanks for sharing. I tidied those posts up 😉

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