Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Custom Search Page Template for Query with No Results WordPress

  • SOLVED

I have an interesting predicament, and I hope that someone can help me get this straightened out.

I will do my best to explain how everything works so that you can help find a solution.

I have both post and page templates. Users can select default template settings in an options menu (content/sidebar, full width, left sidebar, content/sidebar/sidebar, sidebar/content/sidebar, sidebar/sidebar/content). They can select these default templates for posts, pages and the index page. Once applied, the selected template will prevail throughout the site.

Since "Default" is the post/page template in their respective Edit screens, the default setting will mirror whatever the user selects in the theme options menu. However, any post/page/custom post type can be changed from within the edit screen by picking a different template from the dropdown menu in the metabox. This will apply only to the post/page/custom post type selected. The rest of the site will still follow the default setting in the theme options panel. All of this is accomplished through the template_redirect hook.

Ok, so now my predicament: using this system relies on checking the database to see if the get_post_meta variable is set to a specific setting. This works perfectly everywhere EXCEPT for search pages that return no search results. Because they return no search results, there isn't going to be anything in get_post_meta to test against. While the default template will still prevail, no content can be outputted and I get this error:

Trying to get property of non-object

It makes sense that this is happening, but I can't figure out a way around it. Like I said, it works fine on everything (including search pages with results) except for search pages with no results. I can remove the conditional check for get_post_meta, but by doing that there is no way to reference my option set in theme options for output. It will simply default to the default layout (content/sidebar).

Here is an example of the code I am using to render posts:

if ( $inline_options['default_post_layout'] == 'Content / Sidebar / Sidebar' && ! is_page() && ! is_home() ) {

if ( get_post_meta( $post->ID, '_wp_post_template', true ) == '' ) {

$default_template = add_filter( 'body_class', 'inline_post_body_class' );

$default_template = include_once( PARENT_DIR . '/content-sidebar-sidebar-template-post.php' );

exit( $default_template );

}

else {

}

}


Is there a way to set the search page template correctly with the option selected in the theme options menu? Basically, if a search page shows no results, I need it to retain the layout selected in the option and display a "no results found" message (or something like that).

Your help is greatly appreciated! I understand that it may be hard to grasp what I am asking, so if I need to explain more let me know.

Answers (3)

2011-06-15

Utkarsh Kukreti answers:

Couldn't you just disable this for Search pages, just like you're doing it for home and page?

Change

if ( $inline_options['default_post_layout'] == 'Content / Sidebar / Sidebar' && ! is_page() && ! is_home() ) {

to

if ( $inline_options['default_post_layout'] == 'Content / Sidebar / Sidebar' && ! is_page() && ! is_home() && ! is_search() ) {

Edit:

if ( $inline_options['default_post_layout'] == 'Content / Sidebar / Sidebar' && ! is_page() && ! is_home() && ! ( ! isset($post) && is_search() ) ) {


Thomas Griffin comments:

Yes, I can do that. However, I need a way to set the search page with no results to the template specified in the theme options. The templates are activated based on information in the get_post_meta field, which is not active on search pages with no results.


Thomas Griffin comments:

It would probably be helpful to say that this is a framework. Everything is fired up through an init.php file. Each template simply has a call called inline(); that loads everything.


Thomas Griffin comments:

Ok, I got it figured out! Thanks for your code. I used a piece of it to help out! :)

Now I have one final issue that I'd like to get some help with. It seems that my search function isn't working. It works on the index page, but whenever I try to input a search on a post or page, I get a wp-comments.php error:

Error: please type a comment.

Any idea why it would be doing this? I have never seen this error before.


Utkarsh Kukreti comments:

Could you link me to the site? That's a strange error for a search form.


Thomas Griffin comments:

Message sent.

2011-06-15

Nick Parsons answers:

Could you post the code of your search.php?

I'm thinking you could run a check using

if( have_posts() )

in search.php, and then only look for the post meta if there are posts.



Thomas Griffin comments:

I have already done that. The problem is how to specify a template if no post meta exists. The templates are fired based on post meta settings. I need a way to load the post template that has been selected in the theme options if no post meta exists. I would love to use the template_redirect hook again, but by the time I've already gotten to that point, the template_redirect hook has already been fired.

2011-06-15

Peter Michael answers:

I think you could use this

function set_search_template() {
global $wp_query, $_search_template;
if($wp_query->is_search) {
if(have_posts()) {
$_search_template = get_post_meta($wp_query->post->ID, '_wp_post_template', true);;
} else {
$_search_template = 'content-sidebar-sidebar-template-post.php';
}
}
}
add_action( 'wp', 'set_search_template' );

and then use $_search_template in the template_redirect hook. Untested though.


Thomas Griffin comments:

Hey Peter,

I just awarded the money because I got it figured out, but thank you for your answer. My solution is similar to yours. It looks like this:

if ( $inline_options['default_post_layout'] == 'Sidebar / Content / Sidebar' && ( ! isset( $post ) && is_search() ) ) {

apply_filters( 'inline_search_no_result_custom_template', __( add_action( 'inline_alternate_loop', 'inline_search_no_results' ), 'inline' ) );
add_filter( 'body_class', 'inline_search_body_class' );
add_action( 'template_redirect', 'inline_search_no_results_template', 11 );

}

else {

}


With the callback of the action as:

if ( $inline_options['default_post_layout'] == 'Sidebar / Content / Sidebar' ) {

$search_template = include_once( PARENT_DIR . '/sidebar-content-sidebar-template-post.php' );

exit( $search_template );
}


And then the body_class filters, etc. etc. Of course global variables added as well :)