Forums › Forums › Search & Filter Pro › Custom page template
- This topic has 7 replies, 2 voices, and was last updated 5 years, 4 months ago by
Trevor.
-
Anonymous(Private) October 18, 2019 at 12:25 pm #224080
I wonder if you can confirm if I am taking the correct approach here?
I wanted to customise the layout of the whole page, rather than just the search results, so decided to use the custom method rather than a shortcode.
I have created a new page template (mysearchtemplate.php) and applied this to a new page ie. mysite/mysearchpage.
I have created a new search form and set the display to “Custom” and the results page to mysite/mysearchpage.
At the top of the page template (mysearchtemplate.php) I have the following code –
$args['search_filter_id'] = 13184; $query = new WP_Query($args);
Also in the template is the code –
echo do_shortcode("[searchandfilter id='13184']");
In order to display the search form itself.
This does seem to work but it also seems to be quite slow… Am I missing something?
Using the Query Monitor plug-in, I can see that the main query is still shown as
SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND (wp_posts.ID = '13181') AND wp_posts.post_type = 'page' ORDER BY wp_posts.post_date DESC
but I can’t figure out where that’s coming from and/or how I can override it, even if I need to?
Any thoughts at all, or do you even have an example of a basic page template for using this method? (In the theme I’m using, most of the loops seem to be generated in functions so it’s really difficult to track them down, but if I could start from scratch with a basic page template, perhaps I could build it up from there.)
Or maybe I am doing everything right and it’s just generally a slow search? It’s certainly show the correct results at least.
With many thanks in advance for any thoughts.
Trevor(Private) October 18, 2019 at 12:56 pm #224081It is entirely possible that your site is also running its own query, as well as the one you make for our query.
The simplest approach is to use the Shortcode method. In common with the Custom method, it needs a fixed results URL. But, you can then focus on page customization of the loop itself. Your theme should allow you to select the theme template being used for the page, and any theme options, such as sidebars, width etc.
Our documentation covers how to start with customizing the loop, but I would leave that till a bit later, as you might choose to use a loop builder like the Post Grid plugin:
https://searchandfilter.com/documentation/search-results/using-a-shortcode/
https://searchandfilter.com/documentation/3rd-party/post-grid/
Anonymous(Private) October 18, 2019 at 1:18 pm #224085Thank you Trevor and I think you’re very right about the site/theme running its own queries (despite the fact that I’ve already added a load of remove pre_get_posts from the page).
I did already try the shortcode method and it was working OK, but I was struggling with the layout which is why I thought creating a whole new template might be easier…
Just to make sure I’ve understood correctly, does the shortcode method automatically override any other query which might be running ie. essentially creating its own loop?
Also I see that the shortcode uses the basic page.php template but is it possible to explain which part of that template it actually replaces?
Trevor(Private) October 18, 2019 at 1:36 pm #224091Our Shortcode method replaces the query on the page. However, in the order in which WordPress does things, pre_get_posts always takes precedence, which is why, if that is what is happening, we provide a code snippet to override that also.
function pre_get_posts_function($query) { //this would be a pre_get_posts you already have in place somewhere //then set <code>search_filter_id</code> $query->set("search_filter_id", 123); } add_action( 'pre_get_posts', array($this, 'pre_get_posts_function') );
Anonymous(Private) October 18, 2019 at 3:17 pm #224121Thank you Trevor and I’ll bear that in mind, although I don’t think there is a pre_get_posts running the basic page template.
What I’m still trying to understand however, is what part of the page.php template is replaced/overwritten by the results.php in the search-filter folder (when using a shortcode)?
Trevor(Private) October 18, 2019 at 3:21 pm #224125Just the content. So, header, footer and sidebars are not affected. From the WP admin page editor, you put the results shortcode in the content editor to display the results. You can put other stuff in their. Typically, most themes call the container either content or main (usually an ID).
Anonymous(Private) October 18, 2019 at 3:41 pm #224132Thank you again Trevor. I realise that probably should be obvious, although my page.php has a whole lot of other stuff in the section ID=content which clearly doesn’t get replaced by the results.php but now I see it’s specifically the_content which gets replaced, hopefully I can work around that.
Incidentally, I am well aware that my confusion here is not caused so much by your plugin as the (in my opinion) unnecessarily complicated theme I am stuck with… but understanding exactly how S&F works does at least help me to work out the best way to tackle a solution.
-
AuthorPosts