Restrict Search Results to Custom Post Type

Hi there.

Site: https://fantasy-books.live
Wordpress Version: 4.9.4
X Pro: 1.2.7

Problem:

Is it possible for the search results to only look at post from a custom post type?

If not, can you direct me to the file that governs the search function of the theme?

Thanks

J.

Hi there,

Thanks for posting in.

There is no file that you can edit since it’s part of the global query. Please try this one https://thomasgriffin.io/how-to-include-custom-post-types-in-wordpress-search-results/ and set the post type you preferred.

Thanks!

Hi there.

Thanks, it worked great.

Now, I was wondering if there was a place to go to edit how the search results appear?

J.

Hello There,

Thanks for writing in! Please be informed that the search page results is not editable or cannot be edited using Cornerstone or the default WordPress editor. The search results page is dynamically generated by WordPress and the layout is displayed using the index.php template.

Hope this helps.

Hi. Can you guide me to the correct index.php? As in , file path?

Hello There,

index.php file is the main template in a WordPress theme. Please check out this template hierarchy diagram (https://developer.wordpress.org/files/2014/10/wp-hierarchy.png):
https://developer.wordpress.org/files/2014/10/wp-hierarchy.png

You will need to duplicate index.php and named it as your search.php file. You will have to create your own search template overrides as well. Please keep in mind that this particular customization request is outside the scope of our support as this is not related to an issue with the theme and instead has to do with your customization of it. As such, you will need to investigate this particular issue on your own or seek help from a developer should you not feel comfortable making these changes yourself.

Thank you for your understanding.

So where is x theme’s main template file?

Hi there,

X does not have an override of the file, it uses the index.php file at the root which it in its turn depending on the stack you choose brings the wp-content/themes/x/framework/views/{name-of-stack}/wp-index.php.

Thank you.

Thanks.

That’s what I was looking for.

So, if I were to copy it and paste it in my child theme and make changes, it’ll take effect?

J.

Hey There,

Yes it will surely take effect as long as you have made some modifications, placed the file in the child theme and that the child theme is active as your theme.

Hope this helps. Kindly let us know.

Hi. So, there is this <?php x_get_view( 'global', '_index' ); ?>

In the search index that shows the results. So, based on what is best, would you recommend editing the x_get_view, or making my personal loop and editing that?

Hi,

Editing that file will affect all your index page like your blog page.

You can create searchpage.php and copy all the contents of the index file into it.

You can then modify that file as you wish, like adding your personal loop.

https://codex.wordpress.org/Creating_a_Search_Page

Thanks

Thanks! I believe I got it from here then.

You’re most welcome!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.