0

I have a custom post type 'Artist'. I also have individual artist pages for each of my 3 artists. I limited my default wordpress search to search only for 'Artist' post type using this in the functions.php file.

   function searchfilter($query) {

    if ($query->is_search && !is_admin() ) {
    $query->set('post_type',array('Artist'));
    }
    return $query;
    }
    add_filter('pre_get_posts','searchfilter');

Now, How can I make sure, that if I type the artist name, it shows the search results on the artist page and not on the default search page.

Example, if I type John in the search box, the search results should show the all the content of the Artist page which contains the word John.

Here is the search.php code

      <?php
    if ( have_posts() ) : ?>

        <?php 
        /* Start the Loop */
        while ( have_posts() ) : the_post();

            /**
             * Run the loop for the search to output the results.
             * If you want to overload this in a child theme then include a file
             * called content-search.php and that will be used instead.
             */
            <?php the_title(); ?>
            <?php the_content(); ?>

        endwhile;

        the_posts_navigation();

    else :


        get_template_part( 'template-parts/content', 'none' );

    endif; ?>
  • The search results will show all the `artist` pages that have the keywords and not just one artist page. Do you want to show only one page? What do you want to happen if users search for keywords that are in both artist pages? – Amit Joshi Aug 02 '17 at 03:31
  • Hi Amit, So, If I type John, it should show me all the divs on all the pages that contain the word John. If the word john is on both the pages, it should show all the divs of both the pages that contain the word John. Right now I am getting the titles of all the pages that contain the word John –  Aug 03 '17 at 13:36
  • Possible duplicate of [Add ACF fields to search results page WordPress](https://stackoverflow.com/questions/36787391/add-acf-fields-to-search-results-page-wordpress) – FluffyKitten Aug 08 '17 at 04:22

1 Answers1

0

OK so you want to show the contents of the searched posts as well. Querying the posts and showing the results are 2 different things. All the code you wrote here does is to modify the query so that only posts of type Artist are shown. To show the result you must have run the loop on the search results somewhere. Add a line to show the contents there (search for the_content or get_the_content).

Amit Joshi
  • 1,334
  • 1
  • 8
  • 10
  • Please refer to the added code above. It is still showing just the title and not the content. The content I am trying to show is built using ACF. But it aint showing up on search results. –  Aug 03 '17 at 14:58
  • You are giving all the info in such a piecemeal :). If the content is coming from the ACF, then the you need to get the fields from the acf contents. Search for `get_field` and `the_field` now or get the info from https://www.advancedcustomfields.com/resources/get_field/ – Amit Joshi Aug 03 '17 at 15:07
  • and how could I Do that ? I have a custom field "description". If it contains the word John how can I display it on search results page ? –  Aug 03 '17 at 15:15
  • Assuming `description` is the id of your custom field, `the_field('description')` after `the_content();` should do the trick. – Amit Joshi Aug 03 '17 at 15:29
  • You mean like this
    –  Aug 03 '17 at 15:32
  • well yes. Give it a try. – Amit Joshi Aug 03 '17 at 15:44
  • Did that mate. Same result. Just the title and link of all the pages containing the word "John" –  Aug 03 '17 at 15:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/150985/discussion-between-amit-joshi-and-jon-snow). – Amit Joshi Aug 03 '17 at 16:32