• Hello,

    I am working with Custom Content Types Manager and Relevanssi.

    In CCTM I have a custom field that links to other posts and, if I enter as keyword part of one of those posts’ name, I need to see also the posts linked to it.

    This shouldn’t be hard but I noticed that the “external” posts are stored by ID and not by their names.

    I tweakked the search.php page in order to get the ID of the external post but I’m struggling to add this parameter to the search keywords.

    I tried to create a new WP_Query object but it seems that Relevanssi blocks the “s” variable of the query.

    How can I manually add the ID of eventual external posts to grab all the posts?

    http://wordpress.org/plugins/relevanssi/

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

    (@msaari)

    I’m sorry, but I don’t quite understand the problem here.

    Thread Starter Giulio Pecorella

    (@giulio-pecorella)

    Sorry for the delay answering but I was trying to solve it on my own with no results.

    In CCTM, the “Relational” type of Custom Field allows you to create a One-To-One or One-To-Many relation between WordPress Posts.

    This is achieved by storing the related posts in the custom fields via the following syntax:

    [“1st_post_id”,”2nd_post_id”].

    Let’s say I have two custom post types: “Book” and “Writer”.

    I have a book post titled “The Big Story” that has a relational field called “book_writer” and a writer post called “John Doe” whose ID is 42.

    Since John Doe wrote “The Big Story”, I link the writer to the book via the “book_writer” field.

    The content of “book_writer” field will be [“42”].

    If I search “John Doe” I can get the “John Doe” post and any other post that mentions “John Doe” in its title or content but I can’t get the post “The Big Story” because the field hasn’t stored “John Doe” but [“42”].

    Due to naming standards of the project, I cannot add the writer of the book in the title of the book post.

    I edited the search.php file trying to do as follows:

    1) I get the “s” variable of the query to get the keywords and save them into a $new_keywords variable.

    2) If, in the results of the query, there’s a post whose post_type is “writer” I catch its ID and append it to the $new_keywords variable.

    3) I do a new WP_Query passing the $new_keywords variable as the “s” variable.

    Unfortunately the new WP_Query returns no results.

    Is there any other way to get the related posts in Relevanssi or am I getting wrong in the logic of my edit?

    Hope it’s more clear now and thank you

    Plugin Author Mikko Saari

    (@msaari)

    I’d say you’re making it harder than necessary.

    add_filter('relevanssi_content_to_index', 'add_book_writer', 10, 2);
    function add_book_writer($content, $post) {
        $writer = get_post_meta($post->ID, 'book_writer', true);
        if (!empty($writer)) {
            $ids = explode(",", $writer);
            foreach ($ids as $id) {
                $id = str_replace('[', '', $id);
                $id = str_replace(']', '', $id);
                $id = str_replace('"', '', $id);
                $name = get author name by id ($id);
                $content .= $name . " ";
            }
        }
        return $content;
    }

    Replace get author name by id ($id); with whatever it takes to get a name by ID.

    This code gets the related author names before a post is indexed and adds them to the post content. Relevanssi will then index them with the post content and the post can be found by the author name as well.

    Another way to do this would be a hook on wp_insert_post that would fetch the author names and store them in another custom field, which Relevanssi could then index.

    Thread Starter Giulio Pecorella

    (@giulio-pecorella)

    Thank you, this worked almost like a charm.

    Now when I search the writer I can get all of his books.

    I’m still having problems retrieving the author in the search results via “books related” keywords. Perhaps I should re-build the index?

    Plugin Author Mikko Saari

    (@msaari)

    Yes, rebuild the index, that’s important.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Alter the query string.’ is closed to new replies.