2

I am new to WordPress, so I am unsure how to accomplish this. I have a custom post type called, fs_ski_resort I created a template called "Archive: FS Ski Resort". The page has a search box, in the top. I have a query loop and I have unselected "Inherit query from template" and selected fs_ski_resort post type. The page displays, without searching, all fs_ski_resort data (about 300 records) in a custom format.

The problem is it that if I search for one ski resort it returns the results using, I assume, the default search result page (not in the format I made). It stops using the custom results page I made above called "Archive: FS Ski Resort". How can I fix this?

1
  • You have to look into that query, how it searches the CPT fs_ski_resort Apr 15 at 6:39

2 Answers 2

1

Here is one approach to point the form's action of your Search block to any custom FSE page on your site, including the current path.

It should be possible to use the query attribute in the Search block's markup in your archive template:

"query" : { "post_type" : "fs_ski_resort" }

to add the public WP_Query query variable post_type as a hidden HTML input field.

See here:

https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/blocks/search.php#L38

This should restrict search results to the fs_ski_resort post type when using Inherit query from template query-loop option.

The whole markup could look like:

<!-- wp:search {
    "label":"Search",
    "buttonText":"Search", 
    "query" : {"post_type" : "fs_ski_resort"}, 
    "actionPath" : "/change/this/to/your/search/results/path" 
} /-->

To support the custom actionPath attribute above, to be able to change the action part of the form, we use e.g. the following PHP code as a plugin:

add_filter( 'render_block', function( $block_content, $block ) {
    if ( 'core/search' !== $block['blockName'] ) {
        return $block_content;
    }
    if ( empty( $block['attrs']['actionPath'] ) ) {
        return $block_content;      
    }
    $processor = new WP_HTML_Tag_Processor( $block_content );
    if ( $processor->next_tag() ) {
        $processor->set_attribute( 
            'action', 
            esc_url( home_url( '/' ) . ltrim( $block['attrs']['actionPath'], '/' ) )
        );
        return $processor->get_updated_html();
    }
    return $block_content;
}, 10, 2 );

using the new WP_HTML_Tag_Processor to replace the action part of the form to our needs. Note that we append the actionPath input to the home url.

The generated output for the above block markup would be:

<form role="search" method="get" action="https://example.com/change/this/to/your/search/results/path" class="wp-block-search__button-outside wp-block-search__text-button wp-block-search">
    <label class="wp-block-search__label" for="wp-block-search__input">Search</label>
    <div class="wp-block-search__inside-wrapper">
        <input class="wp-block-search__input" id="wp-block-search__input" placeholder="" value="" type="search" name="s" required /><input type="hidden" name="post_type" value="fs_ski_resort" />
        <button aria-label="Search" class="wp-block-search__button wp-element-button" type="submit">Search</button>
    </div>
</form>

Hope you can adjust this further to your needs.

2
  • I would rather not use archive but search-custom_pop_type template. Where does this go: "query" : { "post_type" : "fs_ski_resort" } ? I cannot see where to change this; change/this/to/your/search/results/path in the php? Thank you. Apr 29 at 12:34
  • You add the query and actionPath attributes into the search block markup via the site editor in the code view mode and actionPath should contain the path or slug to any of your results page. Very flexible solution.
    – birgire
    May 1 at 18:51
0

Search results uses a totally different template - not the custom post type archive. So, you'll need to copy the archive template you made to the search results template. See the WordPress documentation for how to do that: https://codex.wordpress.org/Creating_a_Search_Page

That said, default WordPress search will not limit itself to just your single custom post type - it will return results from the whole site. If you want the results to show just your Ski Resort cpt, you'll need to customize the search form itself. This question/answer addresses that to some extent: https://wordpress.stackexchange.com/a/409464/16

Let me know if you need more specifics based on the code you're trying to implement.

4
  • Is the above answer using the "block editor"? It seems quite different when using the block editor. Apr 21 at 0:45
  • Where do I put the php code to limit the results though? Apr 21 at 0:50
  • @spreaderman you'd typically put that code in functions.php in the theme, or in a plugin as mentioned in the other answer.
    – Michelle
    Apr 22 at 15:41
  • In the old way, I could easily make a template like search-fs_ski_resorts Cannot understand how to do this in the block editor. There is lot of info to chance the default search but not for a custom post type. What is this from way above -> "actionPath" : "/change/this/to/your/search/results/path" Apr 26 at 8:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.