Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Mikko Saari

    (@msaari)

    How are artists exactly related to products? Via custom field, but what does that custom field contain? The name of the artist? In that case it should be as simple as having Relevanssi index that custom field. However, if the custom field has an artist ID or something like that, then Relevanssi won’t be able to see the name, and you need to tell it to Relevanssi.

    You can use the relevanssi_content_to_index to add any content you want to posts before Relevanssi sees them. Just write a small function that adds the name of the artist to the post, and you’re set.

    Thread Starter sohels

    (@sohels)

    Hi Mikko,

    Thanks a lot for the quick response. An artist is a custom post type. This is linked to a product via a custom field which contains a ‘post object’. It is a dropdown field on the product admin page with a list of artist names.

    If you don’t mind having a look, I can give you my wp-admin access. Please let me know your email address.

    I have also uploaded relevant screenshots here:

    Relevanssi

    Plugin Author Mikko Saari

    (@msaari)

    Yeah, based on that I’m sure Relevanssi simply doesn’t see the connection to the artist. Relevanssi can only read text in custom fields, it doesn’t understand more complicated connections between different post types and taxonomies.

    You need to use the relevanssi_content_to_index filter to add a small function that, given a post ID, returns the names of the artist related to the product.

    add_filter('relevanssi_content_to_index', 'rlv_add_artist', 10, 2);
    function rlv_add_artist($content, $post_id) {
        $artist = get the name of the artist here($post_id);
        return $artist;
    }

    That does it. How to get the name of the artist based on $post_id –?well, that I can’t tell, exactly.

    Thread Starter sohels

    (@sohels)

    Thanks Mikko, it works now.

    Thread Starter sohels

    (@sohels)

    Hi Mikko,

    A developer recently made some changes and I can no longer search via artist. Unfortunately I’m not able to reach him.

    The following function is still there in functions.php:

    add_filter('relevanssi_content_to_index', 'rlv_add_artist', 10, 2);
    function rlv_add_artist($content, $post_id) {
        $artist = get_post_meta($post->ID, 'artist', true);
        $artist = get_the_title( $artist );
        return $artist;
    }

    Not sure why its not working – can you please take a look: https://motibili.in/

    Try searching for ‘Shikha’.

    Plugin Author Mikko Saari

    (@msaari)

    I would recommend you try to find out what the developer changed. Without knowing that, any searching on your site won’t help me.

    Thread Starter sohels

    (@sohels)

    Hi Mikko,

    I couldn’t reach the developer and the site had a number of issues so I’m rebuilding it from scratch. Most of the stuff is done:

    https://artillery.in

    For some artists, I am able to search by their name, but I suspect this is because Relevanssi has indexed vendor names (vendors are a custom taxonomy). Those artists that are not vendors eg – ‘Sanatan Mehta’, are not searchable.

    Updated function in functions.php:

    //Add artist names to product search content
    add_filter('relevanssi_content_to_index', 'rlv_add_artist', 10, 2);
    function rlv_add_artist($content, $post_id) {
    	if ( get_post_type($post_id) == 'product' ){
        	$artist = get_post_meta($post->ID, 'artist', true);
        	$artist = get_the_title( $artist );
        	return $artist;
    	}
    }

    Thoughts? This type I can answer any question you may have since I’ve done everything myself!

    Sohel

    Plugin Author Mikko Saari

    (@msaari)

    If an artist is a post and the post ID in the meta field “artist” matches the artist post ID, that code should make the artist name searchable.

    If it doesn’t work after you index the database, you need to debug this – print out the $artist in there and see what goes into the database when you rebuild.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Can't search custom post type/custom field’ is closed to new replies.