Wordpress - how to search pages only - wordpress

I need a little help on Wordpress searching. The thing I want to know is how to search Wordpress pages only.

You might want to check out this page on the wordpress forums.
You have to add a hidden form field in your search form and add a hook to update the query that wordpress generates.
Edit: Here's the code from the forum post mentioned above.
In the search form:
<input type="hidden" name="post_type" value="page" />
In functions.php:
function mySearchFilter($query) {
$post_type = $_GET['type'];
if (!$post_type) {
$post_type = 'any';
}
if ($query->is_search) {
$query->set('post_type', $post_type);
};
return $query;
};
add_filter('pre_get_posts','mySearchFilter');

In your search.php, find The Loop and insert this code just after it. You can recognize the Loop because it usually starts with:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>
Code to be inserted:
if (is_search() && ($post->post_type=='post')) continue;
So, your code should be like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>
<?php if (is_search() && ($post->post_type=='post')) continue; ?>
Let me know if it worked.

Related

How to force Wordpress search bar to search pages not products?

First let me say I've hardly ever posted on stackoverflow so I hope I'm in the right section.
I have a Wordpress site with Woocommerce for my client. I want the search bar to NOT search for products and instead search for pages. It's important to ignore all products because we don't order products from product pages, instead I have collections of order forms on multiple pages that are titled with the product name. Can we change the search bar so it picks up on just the pages and not products?
I'm comfortable with working in the functions.php or duplicating Woocommerce files in my child theme. Any help is much appreciated!
[** EDITED/UPDATE **]
I now have this:
function filter_search($query) {
if ($query->is_search) {
$query->set('post_type', array('post', 'page'));
}
return $query;
}
add_filter('pre_get_posts', 'filter_search');
function __search_by_title_only( $search, &$wp_query ){
global $wpdb;
if ( empty( $search ) )
return $search; // skip processing - no search term in query
$q = $wp_query->query_vars;
$n = ! empty( $q['exact'] ) ? '' : '%';
$search =
$searchand = '';
foreach ( (array) $q['search_terms'] as $term ) {
$term = esc_sql( like_escape( $term ) );
$search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term{{$n}')";
$searchand = ' AND ';
}
if ( ! empty( $search ) ) {
$search = " AND ({$search}) ";
if ( ! is_user_logged_in() )
$search .= " AND ($wpdb->posts.post_password = '') ";
}
$search .= " AND ($wpdb->posts.post_type = 'page') ";
return $search;
}
add_filter( 'posts_search', '__search_by_title_only', 500, 2 );
Which is working perfectly. It searches for pages not products and it searches for the search word in a title instead of the content. Now my only request and issue is I need to make it output the page LINK not the page CONTENT. (It outputs the pure content of the page, one after another)
[** EDITED/UPDATE #2 **]
I achieved the same result as above by removing all of that code and copying product-searchform.php (from woocommerce) into my child theme (in a folder called woocommerce) and changing:
<input type="hidden" name="post_type" value="product" />
to
<input type="hidden" name="post_type" value="page" />
This achieves the same affect of querying only pages and not products, as well as searching the keyword in the page titles instead of searching in the page content. Now I am still getting the same output of it displaying the page content and which I only want it to display the link to the page, and not the content. Any ideas?
[** EDITED/UPDATE 3 **]
Okay, I solved this.
Next step is to copy search.php into your child theme and add some code.
Add:
$s=get_search_query();
$args = array('s' =>$s);
// The Query
$the_query = new WP_Query( $args );
Right before
if ( have_posts() ) : ?>
Then add:
while ( $the_query->have_posts() ) {
$the_query->the_post();?>
<li>
<?php the_title(); ?>
</li><?php
}
Right before:
get_template_part( 'loop' );
And comment that get_template_part('loop'); part out.
Try to add this code in functions.php
function filter_search($query) {
if ($query->is_search) {
$query->set('post_type', array('post', 'page'));
};
return $query;
};
add_filter('pre_get_posts', 'filter_search');

Display custom post using ID

In my theme, I have a custom post type called sliders. Here the user is allowed to upload images in a meta-box. So, the images are saved as meta data in wp_post_meta table.
Now what I want to do is, displaying a slider using its ID.
I did like following but no result
$my_query = new WP_Query('post_type=sliders&p=411');
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
the_post();
endwhile;
}
This doesn't display anything. No even errors. Anyway, If I used the_title() instead of the_post(), it shows the title of the slider fine. Same for the the_author() It shows the author without error.
Why is this weird ?
According to Codex the_post(); :
the_post(); iterate the post index in The Loop. Retrieves the next
post, sets up the post, sets the 'in the loop' property to true.
This function does not return any values.
Example for how we use this function:
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
he_title();
the_content();
}
}
?>
You didn't explain well you question. anyway to display your metabox just use API function
echo get_post_meta( $id, 'metabox_name', true );

Conditional statement to check if post is in 2014

How can I check if a post is in a specific year? I need to output some code if the single post is in a specific year.
Inside of the wp loop you can get the date of the post with the built in wp functino of the_date('Y')
So in the wp loop just check to see if the date == something like so.
<?php
if ( have_posts() ) {
while ( have_posts() ) {
if(the_date('Y') == '2015'):
the_post();
else:
continue;
endif;
} // end while
} // end if
?>

Custom Menus Links not working in category page

Custom Menu Links are not displaying in category page. When i checked the source, it shows me like this.
<div class="sidebar-widget">
<h1 class="widget-title">Popular Categories</h1>
<div class="menu-cus_menu-container">
<ul id="menu-cus_menu" class="menu"></ul>
</div>
</div>
The same function i called for my home page. But it is working fine. The following code is in sidebar.php
<?php
if ( is_active_sidebar( 'sidebar-hompage-widget-area' ) && is_home() ) {
dynamic_sidebar( 'sidebar-hompage-widget-area' );
}
if ( is_active_sidebar( 'sidebar-category-widget-area' ) && is_category() ) {
dynamic_sidebar( 'sidebar-category-widget-area' );
}
if ( is_active_sidebar( 'sidebar-left-widget-area' ) ) {
dynamic_sidebar( 'sidebar-left-widget-area' );
}
?>
Please help me to find out right solution or suggest me to find my mistake.
As far as i think you have a Category page not a category.php or archive.php file. is_category() function only works within category.php or archive.php file. Instead of using is_category try use is_page('category').
For more information on is_category() see http://codex.wordpress.org/Function_Reference/is_category.

wordpress group by post type on search page

I would like to display search results grouped by post type. I have regular posts, pages,
and a custom post type of product. How would I accomplish this by editing the below code.
The code below just shows all posts and pages right now.
<?php
while (have_posts()) : the_post();
echo "<h1>";
echo $post->post_type;
echo $post->post_title;
echo "</h1>";
endwhile;
?>
This code alters the original search query to order the results by post-type in the order you select. There are other solutions, but this is the only one i found that doesn't break pagination or requires multiple queries.
add_filter('posts_orderby', 'my_custom_orderby', 10, 2);
function my_custom_orderby($orderby_statement, $object) {
global $wpdb;
if (!is_search())
return $orderby_statement;
// Disable this filter for future queries (only use this filter for the main query in a search page)
remove_filter(current_filter(), __FUNCTION__);
$orderby_statement = "FIELD(".$wpdb - > prefix.
"posts.post_type, 'post-type-c', 'post-type-example-a', 'custom-post-type-b') ASC";
return $orderby_statement;
}
In your case, I'd do two things:
filter the search page's initial query to a particular post type
use one WP_Query call for each remaining post type
For (1), this would go in your functions.php:
<?php
function SearchFilter($query) {
if ($query->is_search && !is_admin()) {
if (isset($query->query["post_type"])) {
$query->set('post_type', $query->query["post_type"]);
} else {
$query->set('post_type', 'product');
}
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
?>
For (2), adapt the code you provided from your template file:
<?php
$s = isset($_GET["s"]) ? $_GET["s"] : "";
$posts = new WP_Query("s=$s&post_type=post");
if ( $posts->have_posts() ) :
while ( $posts->have_posts() ) : $posts->the_post();
echo "<h1>";
echo $post->post_type;
echo $post->post_title;
echo "</h1>";
endwhile;
wp_reset_postdata();
endif;
?>
You can re-use this code for each other post type.
It's best to avoid using query_posts... see querying posts without query_posts (even WordPress devs agree).
You need to alter the post query to reorder things. You would execute this just before you enter the loop. You can read more about query_posts in the Wordpress codex.
http://codex.wordpress.org/Function_Reference/query_posts
global $wp_query;
$args = array_merge( $wp_query->query, array( 'post_type' => array('type1', 'type2') ) );
query_posts( $args );
//the loop

Resources