I want to get the result in search.php include posts and products only, and the posts list will be display in POST tab, Products list in PRODUCT tab.
I tried to use this code for loop but it get all posts and products:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile;?>
<?php endif; ?>
You may want to try adding a post_type argument to the URL.
www.example.com?s=test&post_type=product (shows products)
www.example.com?s=test&post_type=page (shows pages)
This is not exactly what you are asking because this is 2 separate pages (not 1 page with 2 tabs). You can add a hidden input field to a form in order to have the browser automatically append the post type to the URL. Ie.
<form method="get" action="<?= get_bloginfo( 'url' ); ?>">
<input type="hidden" name="post_type" value="product">
<input type="text" name="s" value="<?= get_search_query(1); ?>" placeholder="Search Products...">
<button type="submit">Go</button>
</form>
To show 2 tabs on the same page, you can use search.php and ignore the main query, and simply write two of your own queries by accessing $_GET['s'] (the function, get_search_term(), also accesses $_GET['s'] but can also sanitize it). Alternatively, you could use a page template to write your own queries, showing search results on a different URL (you would have to modify all search forms to go here instead).
When you visit your websites home URL and add ?s= to the URL, WordPress knows to serve search.php and run a "main query" (using WP_Query) behind the scenes. The main query is what you are looping through when you call while( have_posts() ) etc.
Writing your own queries to show search results may make your code less compatible with plugins that modify the main query to show more meaningful search results. It depends on how they do the logic of whether or not to modify a specific query. For example, they may check that the query is a main query and that you are on the search.php page. When you write your own queries using WP_Query or get_posts(), it's not a main query anymore.
WP_Query (or get_posts() which calls WP_Query) should have an argument where you can specify the search term(s) and it may take care of parsing out all the different words and turning it into (somewhat) useful SQL. You should read the docs on WP_Query for more info.
Sorry this is not a full answer but perhaps it will lead you in the right direction.
Related
I am trying to deal with two get_post functions, in single.php the first get_post function is the post from the wordpress, But after that I called get_post function to other post also to use both of them in the same page but after I call the first get_post ( the main post ) I get the only second data and cant reach the first data.
My code called to the second function ( The first is from wordpress post):
$main_post = get_field('main_post');
$main_p = get_post($main_post->ID);
Then I am trying to use the variable $post OR the_title() OR any other functions to get the first post and it always returning the info of the $main_p post
for example
get_the_title( get_post()->ID )
returns the $main_p post id and not the main post of the single.php
any soulutions ?
I may be wrong, but it seems to me that you are trying to post a different post format with normal post format?
I, myself use get_post_format() so it can be styled differently or have different options.
in single.php I use
<!-- checking if there are any blogposts to be shown using have_posts check which is a wordpress function-->
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?> <!-- the correct syntax for while loop in wordpress to show all the blogposts -->
<?php get_template_part('content', get_post_format()); ?>
<?php endwhile; ?>
<?php else : //else stament if there aren't any posts (put inside if before endif)?>
<p><?php __('No Posts Found'); ?></p>
<?php endif; ?> <!-- stop checking for blog posts-->
</div><!-- /.blog-main -->
Inside functions.php I activated the post-formats inside wp_theme_setup() function
add_theme_support('post-formats', array('aside', 'gallery'));
In this case i activated the gallery and aside posts
That way I have 2 different post types on one page
like this
image from my theme blog page
Here is also a video tutorial on post formats from Traversy media
https://www.youtube.com/watch?v=CRa7eiqyiCM&list=PLc5p9nvpdwBlrNU0hr1f0kXPRkh0aGo1Q&index=7
The key reason why your post values are being overwritten, the additional get_post() declarations are overriding the default query. Now, the code in your pastebin is a pretty massive dog's breakfast, so a direct solution is a rather large undertaking (e.g. the indentation is all over the place, the code snippets are less than ideal regarding their readability, etc...). However, I can point you in the right direction for the solution.
When I pull content from another page on my WordPress sites, I avoid using get_post() in favour of declaring a fresh new WP_Query() (that's just my preference), following it up with a wp_reset_postdata() declaration.
Here's an example of multiple queries on a single template in the WordPress codex:
https://codex.wordpress.org/Class_Reference/WP_Query#Multiple_Loops
The key here is the wp_reset_postdata(). I'd recommend looking into it's purpose. It'll save you a lot of grief:
https://codex.wordpress.org/Function_Reference/wp_reset_postdata
Goal/Information
I'm trying to set up a custom search field and custom search results page to display one category of pages. I need these results on a separate, custom search results page so I can customize how the results are displayed without affecting the default search.
For context, I'm setting up a gallery where the user can search for keywords and relevant pages will be displayed as a thumbnail grid, with each thumbnail linking to a page containing more information on the image.
Each page, containing the image and information, is set up as a page (not post) under the category images.
I'm not sure this is relevant, but in a previous project I've set the default search to exclude images pages with the following code in my functions.php (category ID is 30). I've tried removing this code and it didn't affect my problem described below.
/* Exclude a Category from Search Results */
add_filter( 'pre_get_posts' , 'search_exc_cats' );
function search_exc_cats( $query ) {
if( $query->is_admin )
return $query;
if( $query->is_search ) {
$query->set( 'category__not_in' , array( 30 ) ); // Cat ID
}
return $query;
}
What I've Tried
I'm still learning php and have spent many hours on this to no avail. Most recently I tried tweaking some code from this similar question.
My resultant custom search form looks like this:
<form class="search-form" action="<?php echo home_url( '/' ); ?>" method="get">
<input name="s" type="text" placeholder="Search Images" />
<input name="post_type" type="hidden" value="page" />
<input name="category_name" type="hidden" value="images" />
<input alt="Search" type="submit" value="Search" />
</form>
I've added the following code to the top of my search.php to attempt to redirect to another results page, in this case search-page.php, if the post type has been specified:
<?php
// store the post type from the URL string
$post_type = $_GET['post_type'];
// check to see if there was a post type in the
// URL string and if a results template for that
// post type actually exists
if ( isset( $post_type ) && locate_template( 'search-' . $post_type . '.php' ) ) {
// if so, load that template
get_template_part( 'search', $post_type );
// and then exit out
exit;
}
?>
Note: I took this directly from the similar question linked above.
I've duplicated search.php into search-page.php, removing the code above from the latter.
Problem
Now let's do a search in the custom form above for "marble" (a term featured in one of the image pages). The URL looks right (http://example.com/%3C?s=marble&post_type=page&category_name=images), but I get a 404. In addition, normal searches (those not in the custom form) now take me to a white page – not even a 404. When I remove the above code from functions.php, the default search works properly. (Custom search still 404s.)
Other Attempts
I've also read this guide from Smashing Magazine a few times – Building An Advanced WordPress Search With WP_Query – but can't quite pick out the information relevant to me.
I've tried a couple dozen other guides and questions, but most of them deal with searching for a specific post type alone (and not a category) or searching from a radio-button selection of categories. I haven't been able to scrape any working solutions from them.
Are there any tweaks to what I've tried – or completely different solutions altogether – that will help achieve the desired effect? Specifically, how to create a custom search form for one type of page category, leading to a customizable, separate search results page (independent from the default search results page).
Thanks in advance.
I want to include an automatic search results output on my 404 page based on the url that the user has type in.
The problem is that on the usual search.php file, wordpress gets the value from the URL parameter and uses the regular loop like so:
<?php while (have_posts()) : the_post(); ?>
<h1>Search Results</h1>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
How do I get it so that I can manually enter the search term in the code?
Much appreciated.
You want to automatically search with the value from url?
maybe you can show your similiar post with that value, or show them a recent post with search form , using search for your site and google.com.
may be this link could help Built Effective 404 Error Pages
Hope this help,
Trying my best to wrap my head around wordpress search process and results.
Been reading and researching online for about 8 hours straight at this stage and have gotten almost nowhere.
I have a completely custom theme, designed by a friend for a client. I have a search box in a nav bar at the top of the page, inside the header. I'd like the user to be able to search the site's content from that search box in the nav bar at any time. I've found plenty of examples of this being accomplished on other pages, though they often seem to be using a ready-made theme.
Eventually I'd like to be able to understand what's going in the search.php file well enough to edit it along with the searchform.php file so that the search box and results appear unified with the rest of the theme.
At the moment the closest I've gotten to having anything functioning at all is the inclusion of a quantity of search results showing the appropriate number of hits expected, but I still can not figure out how to display a result.
I've been combing through others' page sources and every hit I can google about customising or just explaining the search.php file, and yet all I keep finding is "read the Creating a Search Page" and "it must be a theme issue." This is not gainful information.
My understanding of what's being taught in the "Creating a Search Page" info is that it describes the steps for creating a custom search page that one can hyperlink / permalink to, but which is unrelated to the default search function within the wordpress kit. Indeed, I've successfully created a custom search page, but it behaves simply as a stand-alone page, and is not related to a customised search results page. To customise the look of default search function results, I understand the search.php file must be edited. So far the "what to edit" within that file escapes me, but for now I'd just like to understand why I am seeing the correct number of results, but no actual results.
My search.php file looks like this:
<?php get_header(); ?>
THIS IS THE TOP OF SEARCH.PHP
<section id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php if ( have_posts() ) : ?>
<header class="page-header">
<h1 class="search-title">
<?php echo $wp_query->found_posts; ?> <?php _e( 'Search Results Found For', 'locale' ); ?>: "<?php the_search_query(); ?>"
</h1>
</header><!-- .page-header -->
<?php
// Start the Loop.
while ( have_posts() ) : the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
endwhile;
/*
// Previous/next post navigation.
twentyfourteen_paging_nav();*/
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;
?>
</div><!-- #content -->
</section><!-- #primary -->
THIS IS AFTER THE SECTION OF SEARCH.PHP
<?php
// get_sidebar( 'content' );
// get_sidebar();
get_footer();
*Note: The notes in CAPS are to help me understand what content is coming from where as I try to figure out what each phpiece of the puzzle is doing.
Also, the twentyfourteen_paging_nav line is commented out as it kept spitting back an error. The original search.php was from a (default theme?) folder called "twentyfourteen". My current folder is called "ewgi2014" however, replacing twentyfourteen_paging_nav with ewgi2014_paging_nav spat back the same error, and I can't find any more useful information on the topic. Also, when looking at other sample search.php files I didn't see the paging_nav call, so I tried commenting it out, and was able to at least avoid the error, though I still don't understand it.
Sample output on the page after searching for the term, say, "program" is:
8 Search Results Found For: "program"
...with nothing else written beyond that.
Not entirely relevant, I believe, but my searchform.php looks like this:
<form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>">
<label>
<input type="search" class="search-field" placeholder="search …" value="" name="s" title="Search for:" />
</label>
<input type="submit" class="search-submit" value="Search" />
</form>
I've read and re-read the "Creating a Search Page" page from the Codex so many times I could probably recite it by heart, but haven't found any solutions within.
I've read the stackoverflow hit about "How to display Wordpress search results?" and have tried the code from ThemeShaper, but that throws an error on the shape_content_nav line(s).
I've read this and ... and apparently I can't link to all the other things I've read as this is my first post, but I have been reading a lot, and fiddling, and making very little headway.
Apologies for the long-winded post. Just trying to be thorough and show my research and current point of understanding.
What happens if you put
the_title();
Directly after your while statement?
Each time you loop within the while loop, you are processing each post. Currently, on each loop, you are entering a content-*.php file, where * depends on the post format. Does that file exist?
So I am trying to add a "-none" to a class for a post if it is in a specific category in Wordpress. So like lets say if I am viewing a post that has a category id of 7, i want a certain class titled "example" change to "example-none".
Here's my code:
<div class="example<?= is_category('events') ?'-none':'' ?><?= in_category('7') ?'-none':'' ?>">
The weird thing with the code is, it works in a page when I am viewing all the posts in a specific category. But when I go to an interior post that is in a specific category, the code does not work.
I am using the in_category('7') tag to achieve this on a wordpress sidebar.
Any idea on what I am doing wrong?
I would remove the quotes around the id of the category:
in_category(7)
This should be a number, not a string.
Thanks. i got it working using this code:
<div class="example<? wp_reset_query(); ?><?= in_category(7) ?'-none':'' ?>">