WordPress.org

Ready to get started?Download WordPress

Forums

Relevanssi - A Better Search
[resolved] Custom wp_query (6 posts)

  1. onetrev
    Member
    Posted 1 year ago #

    I have a page with a custom wp_query, as seen below. It pulls in all the child pages of the current page. But I am having trouble figuring out how you can add content from this page to the Relevanssi index, so that the content on this page with the custom query shows in the search results. Seems like relevanssi_do_query($your_query_object) should be the ticket but nothing I have tried with that has worked.

    <?php
        $childpage_query = new WP_Query(
         array( 'posts_per_page' => -1,
                'post_type' => 'page',
                'post_parent' => $post->ID,
                'orderby' => 'menu_order',
                'order' => 'ASC' )
        );
        while ($childpage_query->have_posts()) : $childpage_query->the_post();
    ?>
    
        <div class="glossary-block">
            <?php the_content(); ?>
        </div> <!-- /glossary-block -->
    
    <?php endwhile; // end of the loop. ?>

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

  2. Mikko Saari
    Member
    Plugin Author

    Posted 1 year ago #

    relevanssi_do_query() has nothing to do with this.

    There's a filter you can use: relevanssi_content_to_index can be used to add arbitrary content to posts before they are indexed. So, for this page, you'd have to create a function that adds the post content from the child posts to the post content.

  3. onetrev
    Member
    Posted 1 year ago #

    Awesome thanks for this. I was totally on the wrong path!

    But if you can quickly help me one last time Mikko, I'm still not sure how to get this to work full. The empty quotes in the filter definition in your list of filters I don't understand either. I would have thought tons of people would have this request as writing a custom query is very common, especially for things like ecommerce sites or anything using CPT.

    In my functions.php file I did this:

    add_filter('relevanssi_content_to_index', 'custom_query_content');
    function custom_query_content($content_data) {
        return $content_data;
    }

    And then within the while loop for my custom query I simply did this:

    <?php
      $content_data = get_the_content();
      custom_query_content($content_data);
    ?>

    Any chance you can assist or does anyone else have any tips? Thanks!

  4. Mikko Saari
    Member
    Plugin Author

    Posted 1 year ago #

    No, that's not correct - the custom_query_content() function you wrote currently does exactly nothing.

    Do something like this:

    function custom_query_content($content) {
        global $post;
        $childpage_query = get_posts(
            array( 'posts_per_page' => -1,
                'post_type' => 'page',
                'post_parent' => $post->ID,
                'orderby' => 'menu_order',
                'order' => 'ASC' )
           );
        foreach ($childpage_query as $t_post) {
        $content .= $t_post->post_content;
        }
        return $content;
    }

    Now, when the indexer calls the filter, the function will run, fetch the extra content and add it to the post content.

  5. onetrev
    Member
    Posted 1 year ago #

    Awesome, huge thanks on that! Totally get it now, this is very handy, and certainly will help some folks. I do not yet need the full version of Relevanssi, but I wish you had a donation option for folks to show their appreciation of the free version of the plugin and your support for it.

    Just a reminder though to anyone else reading this, you now also have to use this filter 'relevanssi_excerpt_content ($content, $post, $query)' in order to have the content from this section show up in the search results excerpts.

    Thanks again!

  6. Mikko Saari
    Member
    Plugin Author

    Posted 1 year ago #

    I do accept donations, my PayPal account is mikko @ mikkosaari.fi. Glad you got this sorted out.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic