I am using the naked word press theme to build my site with and it all works great apart from when I use the search bar. I type in my search and it takes me to a version of my homepage, but with no content on it.
How do I get my search results to display?
Create a search.php and use this code as an example.
<?php get_header(); ?>
<div id="content">
<?php if (have_posts()) : ?>
<h1>Search Result for <?php /* Search Count */ $allsearch = &new WP_Query("s=$s&showposts=-1"); $key = wp_specialchars($s, 1); $count = $allsearch->post_count; _e(''); _e('<span class="search-terms">'); echo $key; _e('</span>'); _e(' — '); echo $count . ' '; _e('acapellas'); wp_reset_query(); ?></h1>
<ul>
<?php while (have_posts()) : the_post(); ?>
<li>
<h2><?php the_title(); ?></h2>
<p><?php the_excerpt(); ?></p>
</li>
<?php endwhile; endif; ?>
</ul>
</div>
<?php get_footer(); ?>
Make sure your searchform.php complies with Wordpress's standards, heres an example.
<form method="get" class="search-form" action="<?php echo home_url( '/' ); ?>">
<label>
<span class="screen-reader-text"><?php echo _x( 'Search:', 'label' ) ?></span>
<span>Search</span>
<input type="search" class="search-field" placeholder="Search" value="<?php echo get_search_query() ?>" name="s" title="<?php echo esc_attr_x( 'Search:', 'label' ) ?>" />
</label>
<button type="submit" class="search-submit" value="<?php echo esc_attr_x( 'Search', 'submit button' ) ?>">
</button>
</form>
Related
I am having problems with the search form on wordpress. When i try and search any term at all it gives me the "Sorry, no posts match your query" response (I have "Search Everything" plugin installed too). I am using a child theme. so I tried to change the code in the child search.php (copied it to the child) and also created a searchform.php in the child. Here is the code from both of those that I am currently using:
SEARCH.PHP:
> <?php
/**
* Search Template
*
* The search template is used to display search results from the native WordPress search.
*
* If no search results are found, the user is assisted in refining their search query in
* an attempt to produce an appropriate search results set for the user's search query.
*
* #package WooFramework
* #subpackage Template
*/
get_header();
global $woo_options;
?>
<!-- #content Starts -->
<?php woo_content_before(); ?>
<div id="content" class="col-full">
<div id="main-sidebar-container">
<!-- #main Starts -->
<?php woo_main_before(); ?>
<section id="main" class="col-left">
<header class="page-header">
<h1 class="page-title"><?php printf( __( 'Search Results for: %s', '' ), get_search_query() ); ?></h1>
</header><!-- .page-header -->
<?php
$query = new WP_Query( array( 's' => get_query_var('s')) );
// The Loop
if ( $query->have_posts() ) {
echo '<ul>';
while ( $query->have_posts() ) {
$query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
}
?>
<?php var_dump($the_query) ?>
</section><!-- /#main -->
<?php get_sidebar(); ?>
</div><!-- /#main-sidebar-container -->
<?php get_sidebar( 'alt' ); ?>
</div><!-- /#content -->
<?php woo_content_after(); ?>
<?php get_footer(); ?>
SEARCHFORM.PHP (I got from the twenty twelve theme):
<form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<label for="s" class="assistive-text"><?php _e( 'Search', 'twentyeleven' ); ?></label>
<input type="text" class="field" name="s" id="s" placeholder="<?php esc_attr_e( 'Search', 'twentyeleven' ); ?>" />
<input type="submit" class="submit" name="submit" id="searchsubmit" value="<?php esc_attr_e( 'Search', 'twentyeleven' ); ?>" />
</form>
I have tried different code for the searchform.php too such as:
<form method="get" id="searchform" action="<?php bloginfo('url'); ?>/">
<label class="hidden" for="s"><?php _e('Search:'); ?></label>
<input type="text" value="<?php the_search_query(); ?>" name="s" id="s" />
<input type="submit" id="searchsubmit" value="GO" />
</form>
and
<form role="search" method="get" id="searchform" class="searchform" action="<?php esc_url( home_url( '/' )); ?>">
<div>
<label class="screen-reader-text" for="s"><?php _x( 'Search for:', 'label' ); ?></label>
<input type="text" value="<?php get_search_query(); ?>" name="s" id="s" />
<input type="submit" id="searchsubmit" value="<?php esc_attr_x( 'Search', 'submit button' ); ?>" />
</div>
</form>
and
<form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>">
<label>
<span class="screen-reader-text">Search for:</span>
<input type="search" class="search-field" placeholder="Search …" value="" name="s" title="Search for:" />
</label>
<input type="submit" class="search-submit" value="Search" />
</form>
But still nothing. When it searches, this is what i get in the url:
http://www.energyhealing.directory/?s=energy&submit=Search
and on the page for that URL it displays:
Search Results for: energy << the title for it
Listed in
Continue Reading << thats a link to nowhere, just comes back to the same page
If someone could just explain to me where I have gone wrong and why, that would be amazing?
Thanks in advance for any help you can give. I look forward to hearing back from you!
Aww ok I see how to post in the question now:
Here is the code from the search.php which just returns the blank page:
<?php
/**
* Search Template
*
* The search template is used to display search results from the native WordPress search.
*
* If no search results are found, the user is assisted in refining their search query in
* an attempt to produce an appropriate search results set for the user's search query.
*
* #package WooFramework
* #subpackage Template
*/
get_header();
global $woo_options;
?>
<!-- #content Starts -->
<?php woo_content_before(); ?>
<div id="content" class="col-full">
<div id="main-sidebar-container">
<!-- #main Starts -->
<?php woo_main_before(); ?>
<section id="main" class="col-left">
<header class="page-header">
<h1 class="page-title"><?php printf( __( 'Search Results for: %s', '' ), get_search_query() ); ?></h1>
</header><!-- .page-header -->
<?php
$query = new WP_Query( array( 's' => get_query_var('s')) );
// The Loop
if ( $query->have_posts() ) {
echo '<ul>';
while ( $query->have_posts() ) {
$query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
}
?>
</section><!-- /#main -->
<?php get_sidebar(); ?>
</div><!-- /#main-sidebar-container -->
<?php get_sidebar( 'alt' ); ?>
</div><!-- /#content -->
<?php woo_content_after(); ?>
<?php get_footer(); ?>
EDITED (for search.php):
<?php if ( have_posts() ):?>
<ul>
<?php while ( have_posts() ) : the_post();?>
<li><?php the_title();?></li>
<?php endwhile;?>
</ul>
<?php endif;?>
I think the searchform.php is ok, but I don't see a loop anywhere in your search.php
$query = new WP_Query( array( 's' => get_query_var('s')) );
// The Loop
if ( $query->have_posts() ) {
echo '<ul>';
while ( $query->have_posts() ) {
$query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
}
You need this query to create a loop based on the search.
I have researched this problem and still can't seem to figure out why my search function isn't working on a theme I'm working on. Below is my code that I have for my search.php and my searchform.php files. The search files were hardly modified from a boilerplate, blank WordPress theme. When I search for anything, it returns a 404 error. I have researched search's returning 404's and still can't find the solution. Blow is my code, any help would be greatly appreciated.
searchform.php----------------------------------
<form action="<?php bloginfo('siteurl'); ?>" id="searchform" method="get">
<label for="search"><object data='<?php bloginfo('template_directory');? >/images/input-search.svg'>
<img src='<?php bloginfo('template_directory');?>/images/input-search.png'>
</object></label>
<input type="search" name="search" />
</form>
search.php---------------------------------------
<?php get_header(); ?>
<?php if (have_posts()) : ?>
<h2>Search Results</h2>
<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
<?php while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>
<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
<?php else : ?>
<h2>No posts found.</h2>
<?php endif; ?>
You need to make sure that your search form is enabled in your functions.php file like so:
// ENABLES SEARCH FORM STUFF
function my_search_form( $form ) {
$form = '<form role="search" method="get" id="searchform" action="' . home_url( '/' ) . '" >
<div><label class="screen-reader-text" for="s">' . __('Search for:') . '</label>
<input type="text" value="' . get_search_query() . '" name="s" id="s" />
<input type="submit" id="searchsubmit" value="'. esc_attr__('Search') .'" />
</div>
</form>';
return $form;
}
// ENABLES SEARCH FORM
add_filter( 'get_search_form', 'my_search_form' );
This is the code I use in my themes. Then this would call in my search form:
<?php get_search_form(); ?>
This is my searchform.php
<form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<label for="s" class="assistive-text"><?php _e( 'Search' ); ?></label>
<input type="text" class="field" name="s" id="s" placeholder="<?php esc_attr_e( 'Search' ); ?>" />
<input type="submit" class="submit" name="submit" id="searchsubmit" value="<?php esc_attr_e( 'Search' ); ?>" />
</form>
I am creating a wordpress template in which I want to integrate a simple search form. I googled a lot about this in the last 3 days and tried several tutorials but I am afraid there is something I don't get at all. I have 3 pages for the search: search.php, searchform.php and searchpage.php. All the tutorials I read provide similar code like this:
search.php:
<?php if (have_posts()) : ?>
…
<?php while (have_posts()) : the_post(); ?>
…
<?php endwhile; else: ?>
… <p>The key word <strong><?php the_search_query(); ?></strong> is not on this website.</p>
<?php include (TEMPLATEPATH . "/searchform.php"); ?>
<?php include (TEMPLATEPATH . "/searchpage.php"); ?>
<?php endif; ?>
searchform.php:
<?php
$querystring = esc_attr(apply_filters('the_search_query', get_search_query()));
$searchstring = "Suchbegriff eingeben";
if (empty($querystring)) { $querystring = $searchstring; }
?>
<form method="get" id="searchform" action="<?php bloginfo('url'); ?>/">
<div>
<input type="text" name="s" id="s" value="<?php echo $querystring; ?>"
onblur="if (this.value == '') { this.value = '<?php echo $searchstring; ?>'; }"
onfocus="if (this.value == '<?php echo $searchstring; ?>') { this.value = ''; }" />
<input type="submit" id="searchsubmit" value="Suchen" />
</div>
</form>
searchpage.php (adapted from page.php):
<?php
/*
Template Name: Search Page
*/
?>
<div id="content">
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div class="blogpost">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php $this->posts = $wpdb->get_results($this->request); ?>
</div> <!-- end class blogpost -->
<?php endwhile; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
If there is no match with the key word the search works. But if there is a match I only get … … … … … … … as output. I know that must be from the search.php but I have no idea how I can change that. Thanks for your advise and help, I really appreciate it!
The important file is search.php, which displays any matching results, or a failure message if there are no matches. Try something like this:
<?php
/**
* Search results page
*/
?>
<?php if ( have_posts() ): ?>
<h2>Search Results for '<?php echo get_search_query(); ?>'</h2>
<ol>
<?php while ( have_posts() ) : the_post(); ?>
<li>
<h2><?php the_title(); ?></h2>
<time datetime="<?php the_time( 'Y-m-d' ); ?>" pubdate><?php the_date(); ?> <?php the_time(); ?></time> <?php comments_popup_link('Leave a Comment', '1 Comment', '% Comments'); ?>
<?php the_content(); ?>
</li>
<?php endwhile; ?>
</ol>
<?php else: ?>
<h2>No results found for '<?php echo get_search_query(); ?>'</h2>
<?php endif; ?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
What is wrong with my code that it results in page going to the previous post after submitting a comment?
This is the webpage.
You may try if you want.
This is the code from comments.php:
<?php
if (!empty($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME']))
die ('Please do not load this page directly. Thanks!');
if ( post_password_required() ) { ?>
This post is password protected. Enter the password to view comments.
<?php
return;
}
?>
<?php if ( have_comments() ) : ?>
<h2 id="comments"><?php comments_number('No Responses', 'One Response', '% Responses' );?></h2>
<div class="navigation">
<div class="next-posts"><?php previous_comments_link() ?></div>
<div class="prev-posts"><?php next_comments_link() ?></div>
</div>
<ol class="commentlist">
<?php wp_list_comments(); ?>
</ol>
<div class="navigation">
<div class="next-posts"><?php previous_comments_link() ?></div>
<div class="prev-posts"><?php next_comments_link() ?></div>
</div>
<?php else : // this is displayed if there are no comments so far ?>
<?php if ( comments_open() ) : ?>
<!-- If comments are open, but there are no comments. -->
<?php else : // comments are closed ?>
<p>Comments are closed.</p>
<?php endif; ?>
<?php endif; ?>
<?php if ( comments_open() ) : ?>
<div id="respond">
<h2><?php comment_form_title( 'Leave a Reply', 'Leave a Reply to %s' ); ?></h2>
<div class="cancel-comment-reply">
<?php cancel_comment_reply_link(); ?>
</div>
<?php if ( get_option('comment_registration') && !is_user_logged_in() ) : ?>
<p>You must be logged in to post a comment.</p>
<?php else : ?>
<form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform">
<?php if ( is_user_logged_in() ) : ?>
<p>Logged in as <?php echo $user_identity; ?>. Log out »</p>
<?php else : ?>
<div>
<input type="text" name="author" id="author" value="<?php echo esc_attr($comment_author); ?>" size="22" tabindex="1" <?php if ($req) echo "aria-required='true'"; ?> />
<label for="author">Name <?php if ($req) echo "(required)"; ?></label>
</div>
<div>
<input type="text" name="email" id="email" value="<?php echo esc_attr($comment_author_email); ?>" size="22" tabindex="2" <?php if ($req) echo "aria-required='true'"; ?> />
<label for="email">Mail (will not be published) <?php if ($req) echo "(required)"; ?></label>
</div>
<div>
<input type="text" name="url" id="url" value="<?php echo esc_attr($comment_author_url); ?>" size="22" tabindex="3" />
<label for="url">Website</label>
</div>
<?php endif; ?>
<!--<p>You can use these tags: <code><?php echo allowed_tags(); ?></code></p>-->
<div>
<textarea name="comment" id="comment" cols="58" rows="10" tabindex="4"></textarea>
</div>
<div>
<input name="submit" type="submit" id="submit" tabindex="5" value="Submit Comment" />
<?php comment_id_fields(); ?>
</div>
<?php do_action('comment_form', $post->ID); ?>
</form>
<?php endif; // If registration required and not logged in ?>
I read this question but don't understand what the solution is.
What do I do to get <?php do_action( 'comment_form', $post->ID ); ?> to behave as expected?
UPDATE:
This is my single.php and as you see, $post is set so don't know what the issue can be:
<?php get_header(); ?>
<nav>
<?php wp_nav_menu(array('menu' => 'Main Nav Menu')); ?>
</nav>
<div id="main-content">
<div class="post-bg-single">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div class="post-title-single"><h2><?php the_title(); ?></h2></div>
<h5 class="date-single">Posted on <?php the_time(get_option('date_format')); ?></h5>
<div class="separator-up-single"></div>
<div class="thumbnail-single"><?php
if( has_post_thumbnail() ){
echo get_the_post_thumbnail(); // or just simply, the_post_thumbnail()
}
?></div>
<div class="entry">
<?php the_content(); ?>
<h5 class="expand">Expand post area</h5>
<!-- <h5 class="print">Printable version</h5> -->
</div>
<div class="separator-down"></div>
<h5 class="categories">Categories: <?php the_category(', ') ?></h5>
<h5 class="tags"><?php the_tags('Tags: ', ', ', '<br />'); ?></h5>
<h5 class="edit"><?php edit_post_link(__('→ Edit post← '), ''); ?></h5>
</div>
</div>
<h84>Read more:</h84>
<div id="related-posts">
<?php
$categories = get_the_category($post->ID);
if ($categories) {
$category_ids = array();
foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
$args=array(
'category__in' => $category_ids,
'post__not_in' => array($post->ID),
'showposts'=>4, // Number of related posts that will be shown.
'caller_get_posts'=>1
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
echo '
<ul>';
while ($my_query->have_posts()) {
$my_query->the_post();
?>
<?php $bg_image = post_thumb( get_the_post_thumbnail() );?>
<a href="<?php the_permalink() ?>"><div class="post-bg-archive" style="background: #777777 url(<?php echo $bg_image;?>);">
<div id="title-bg">
<div class="transparency"></div>
<div class="archive-title"><h10><?php the_title(); ?></h10></div>
</div>
</div></a>
<?php
}
echo '</ul>';
}
}
?>
</div>
<div class="comments-bg">
<?php comments_template(); ?>
</div>
<?php endwhile; endif; ?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
When you do a new WP_Query, it overwrite the global $post variable with the new query.
Either try get_posts() and rewriting the query or add in wp_reset_query(); right before calling comments_template();
wp_reset_query(); would be the fastest way to get it displaying properly.
I've tried a couple of methods but I cannot seem to filter custom post_types from my search results and was hoping someone could help.
I have installed "Job Manager" and created 4 jobs which have a custom post_type = 'jobman_job'
I tried to create a manual search form and set a hidden value of post_type = jobman_job but it still returned all posts.
<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
<input type="text" name="s" id="s" value=""/>
<input type="hidden" name="post_type" value="jobman_job" />
<input type="submit" id="searchsubmit" value="Search" />
</form>
I then tried creating a custom search page and redirecting the search to this page as follows (i.e added page_id hidden field):
<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
<input type="text" name="s" id="s" value=""/>
<input type="hidden" name="page_id" value="123" />
<input type="hidden" name="post_type" value="jobman_job" />
<input type="submit" id="searchsubmit" value="Search" />
</form>
And then in the custom search page, I added the following code (as per wordpress guide - http://codex.wordpress.org/Creating_a_Search_Page) and I added the post_type of jobman_job to the query array:
global $query_string;
$query_args = explode("&", $query_string);
$search_query = array('post_type' => 'jobman_job');
foreach($query_args as $key => $string) {
$query_split = explode("=", $string);
$search_query[$query_split[0]] = urldecode($query_split[1]);
} // foreach
$search = new WP_Query($search_query);
And it still displays all posts...
What am I doing wrong? I have checked the post_type column in the wp_posts table and I have 4 unique entries...so they are there...
Any Insight?
As codex explains, after getting new data you need to replace the loop with your new data, like in this example
<?php if ($pageposts): ?>
<?php global $post; ?>
<?php foreach ($pageposts as $post): ?>
<?php setup_postdata($post); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
<?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
<div class="entry">
<?php the_content('Read the rest of this entry »'); ?>
</div>
<p class="postmetadata">Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>
<?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
</div>
<?php endforeach; ?>
<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php include (TEMPLATEPATH . "/searchform.php"); ?>
<?php endif; ?>
Displaying posts from custom query
I simply left the html as is:
<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
<input type="text" name="s" id="s" value=""/>
<input type="hidden" name="post_type" value="jobman_job" />
<input type="submit" id="searchsubmit" value="Search" />
</form>
and added the following to my functions.php
function mySearchFilter($query) {
if (isset($_GET['post_type']) && $_GET['post_type'] == 'jobman_job') {
$post_type = 'jobman_job';
} else {
$post_type = 'any';
}
if ($query->is_search) {
$query->set('post_type', $post_type);
};
return $query;
};
add_filter('pre_get_posts','mySearchFilter');