• sergio79

    (@sergio79)


    Hi Mikko, I need some advice on a use case.

    My WP has various cpts, one of them is “products” which is composed only of a title and an acf containing a Product ID. I need this to connect this cpt to an external management system.

    When I do a search of course my text data is just the title. To overcome this problem and also allow text searches on other fields (of the external management system), I have developed my own PHP function that searches the external DB and responds with an array of IDs based on the search string received. I then built my own search page where I created a new WP_Query and as args:

    ‘post__in’ => $myFunction(),

    This works. I would like to know if I can also insert this operating logic into the wordpress search (with relevanssi), naturally the IDs received from my function are the IDs of the wordpress “products” cpt (not of my management system), and this filter for “post_in ” should only be done on the “products” cpt.

    Thanks in advance ??

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

    (@msaari)

    The best way to leverage Relevanssi would be to create a function that returns the searchable content of a product given the product ID, and hook that to relevanssi_content_to_index. That way, you would get the product information in the Relevanssi index, and no other tricks are required.

    Thread Starter sergio79

    (@sergio79)

    Thanks Mikko, your support is always special.
    With relevanssi_content_to_index I will then make a SELECT query on all the fields of my DB/Table external to WP, and these data/fields will be automatically inserted into the index so that relevanssi can do searches on it? Is the association of this external data done automatically based on the post-ID?

    Plugin Author Mikko Saari

    (@msaari)

    Yes, with relevanssi_content_to_index you can add whatever content you want from the external DB to the posts, and after that those posts will be found with that content. The association isn’t automatic; you have to create a function that does it. Once you’ve created the function, everything happens automatically – whenever a post is saved or indexed otherwise, Relevanssi will run your function that fetches the external database table content and adds it to the Relevanssi index.

    Thread Starter sergio79

    (@sergio79)

    Hi Mikko, fantastic, it’s the function I needed, it works great!

    I still have some doubts:

    1) Can I make a single select on my external DB by concatenating the 3 fields I need (short description, description, keyword), filtering by $post->ID and putting everything in $content?
    2) I use WPML on my site, should I differentiate the contents taken from the external DB based on the language?

    Thanks as usual!

    Plugin Author Mikko Saari

    (@msaari)

    Relevanssi wants a single string that’s added to the post content. It does not care about how many database requests you need to create that string. The less the better for performance, so if you can get the data with just one query, that’s good.

    In WPML, each post ID is in one language, so yes, your external DB contents should match the language of the current post ID.

    Thread Starter sergio79

    (@sergio79)

    Ok perfect, so I better get the language from $post->ID (of relevanssi_content_to_index) and then make my query to have the content only in that language, fantastic approach!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘post__in filter for cpt’ is closed to new replies.