Custom Post Type Search Template - php

Currently I have 1 custom post type called 'guides' and a couple of custom fields. Now I want to display a search form on archive-guides.php where you people can search on the custom field champion.
This is my code in archive-guides:
<form id="searchform" action="<?php bloginfo('home'); ?>/" method="get">
<input id="s" maxlength="150" name="s" size="20" type="text" value="" class="txt" />
<input name="post_type" type="hidden" value="guides" />
<input id="searchsubmit" class="btn" type="submit" value="Search" />
</form>
This is my code in search.php:
<?php
$search_refer = $_GET["post_type"];
if ($search_refer == 'guides') {
if ('meta_key=champion&meta_value=$search_query') { load_template(TEMPLATEPATH . '/search-guides.php'); } };
?>
But It still wouldnt redirect to search-guides.php, if I search with the form the url will become /?s=query&post_type=guides (query = search term).
Any help appreciated!

I think https://wordpress.stackexchange.com/questions/89886/how-to-create-a-custom-search-for-custom-post-type post will help you to create custom search for custom post type.
Also helpful: http://www.wpbeginner.com/wp-tutorials/how-to-create-advanced-search-form-in-wordpress-for-custom-post-types/

Please update with your code for load_template.

Related

WordPress. Custom search form query not working from custom post_type only

In my WordPress v5.5.1, I have custom post 'song'.
I have the below search form in my header for all pages:
<form>
<input type="hidden" value="song" name="post_type" id="post_type" />
<input type="text" name="s" id="search" value="<?php the_search_query(); ?>">
</form>
And the search results are shown in search.php.
The search form works in all pages except the custom post_type page www.example.com/song/song-1, search query goes to 404 page not found.
Found that upon adding get and action to the form tag as below, it solves the issue:
<form class="search-form" method="get" action="<?php echo bloginfo('url') ?>">
<input type="hidden" value="song" name="post_type" id="post_type" />
<input type="text" name="s" id="search" value="<?php the_search_query(); ?>">
</form>

How can I add custom search box on page?

I'm using custom WordPress template and I don't know how to add search box in a specific place. Of course my template contains search box, but it is in a weird place, so I would like to add custom one. I cannot do that by adding widget. I have to edit index.php to add search box.
When I add this:
<form method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
<div><input type="text" size="18" value="<?php echo wp_specialchars($s, 1); ?>" name="s" id="s" />
<input type="submit" id="searchsubmit" value="Search" class="btn" />
</div>
</form>
Basically, it is working. It is searching properly, but the search box is bad looking.
That one works as well:
<form method="get" id="search_form" action="<?php bloginfo('home'); ?>"/>
<input type="text" class="text" name="s" value="SEARCH" >
<input type="submit" class="submit" value="" />
</form>
But again, it looks awfull. I have to have custom css too.
And another one:
<?php get_search_form(); ?>
Works good, but the same problem. It is just like in 2000.
I tried adding custom css, but the problem is that id does not not searching anything on my website. Instead going to http://example.com/?s=WHAT-I-TYPED it goes to http://example.com/
What can I do with that?
To solve the UGLYNESS problem: Just add the plugin 'simple custom css': https://nl.wordpress.org/plugins/simple-custom-css/. Then write some CSS to make it pretty.
To solve the SEARCH problem replace name="search" with name="s" on your input field (source: https://developer.wordpress.org/reference/functions/get_search_form/).

How to make placeholder for search input in WordPress

I've override default wordpress search form by creating my own search form (found this code in google). here it is:
<form method="get" class="searchform" action="<?php bloginfo('url'); ?>">
<div>
<input class="text" type="text" value="<?php if(trim(wp_specialchars($s,1))!='') echo trim(wp_specialchars($s,1));else echo ' ';?>" name="s" id="s" />
</div>
</form>
I tried to add a place holder for this & change my code for input line to this:
<input class="text" type="text" value="<?php if(trim(wp_specialchars($s,1))!='') echo trim(wp_specialchars($s,1));else echo ' ';?>" name="s" id="s" placeholder="Search Here" />
but it is not working for me. please help someone.
you can simply use the placeholder attribute like so:
<input type="text" placeholder="Search the site"/>
If you put a value, the value will overwrite your placeholder. Try to delete the value from your input and it should work
You can just use placeholder on your input tag like
<input type="text" placeholder="Search the site"/>

Merge wordpress searchform code with custom html code

How do I make this code (custom):
<div id="sb-search" class="sb-search">
<form>
<input class="sb-search-input" placeholder="Search.." type="text" value="" name="search" id="search">
<input class="sb-search-submit" type="submit" value="">
<span class="sb-icon-search"></span>
</form>
</div>
retrieve search results like this one ( wordpress ) does:
<form method="get" class="searchform" action="<?php echo home_url('/'); ?>">
<div>
<input type="text" class="search" name="s" onblur="if(this.value=='')this.value='<?php _e('To search type and hit enter','typegrid'); ?>';" onfocus="if(this.value=='<?php _e('To search type and hit enter','typegrid'); ?>')this.value='';" value="<?php _e('To search type and hit enter','typegrid'); ?>" />
</div>
Basically I got this custom code for an awesome search box with on-click slide etc. It has a field where people can type, but it gets no results as used on my wordpress site. I wanna make it able to get results from wordpress content w/o changing divs thus not altering its design. Please help, I'm stuck!!
This would do it:
<div id="sb-search" class="sb-search">
<form action="<?php echo home_url('/'); ?>">
<input class="sb-search-input" placeholder="Search.." type="text" value="" name="s" id="search">
<input class="sb-search-submit" type="submit" value="">
<span class="sb-icon-search"></span>
</form>
</div>
All you have to do is add the form action, and make the name of the search input to be "s".
<form method="get" class="searchform" action="<?php echo home_url('/'); ?>" >
<input class="sb-search-input" placeholder="Search.." type="text" value="" name="s" id="search">
<input class="sb-search-submit" type="submit" value="">
<span class="sb-icon-search"></span>
</form>
homeurl ?s=anything will call wordpress search function, so just make form's action GET and ACTION = your home url

how to pass values from one page to another on jquery form submit

I'm trying to build a form using php & jquery, but I'm a little confused as to what to do with the jquery portion of it...
Basically, when the user submits the first form, I want to direct them to the "next step" form, but I want to retain the values submitted from the first one in a hidden input field...
If someone can either show me how or point me to a good tutorial, I'd appreciate it...
I don't have any of the php or jquery yet, and this is just a simplified version of the html markup...
//first.php
<form name="form1" method="post" action="second.php">
<input type="text" name="name" value="" />Name
<input type="submit" name="step1" value="Next" />
</form>
//second.php
<form name="form2" method="post" action="process.php">
<input type="hidden" name="name" value="{$_POST['name']}" />
<input type="text" name="message" value="" />message
<input type="submit" name="step2" value="Finish" />
</form>
<input type="hidden" name="name" value="{$_POST['name']}" />
should be,
<input type="hidden" name="name" value="<?php echo $_POST['name']}; ?>" />
and also sanitize the input, if you want
I don't no if there is a better way to do that.
But, when I need to do such thing, I do in this way:
<script>
<?php
foreach($_POST as $key => $valule)
{
echo "$('$key').val('$value')";
}
?>
</script>
So, in your nextstep file, all you'll need to do is set up the hidden fields and then just loop through the post vars and set each one via jquery.

Categories