• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 181
  • Last Modified:

WP Query

Hi guys,

Whats the best way to search  a custom post for a key word

I have this so far

$temp = $wp_query;
								//$wp_query= null; 
								
				if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
				elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
				else { $paged = 1; }
				
				$args = array('post_type' => 'business','posts_per_page'=>9,'order' => 'ASC','paged'=>$paged);
				
				
				if(isset($_GET['keyword']) && $_GET['keyword'] !=='')
				{
					
				}
				
				
				
				
				
				
				$wp_query = new WP_Query($args); 
			
				while ($wp_query->have_posts()) : $wp_query->the_post();
				
					$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
					if($thumbnail[0])
					{
						global $thumbnail;
						global $post;

Open in new window

0
john_yourspace
Asked:
john_yourspace
  • 3
  • 3
1 Solution
 
eemitCommented:
You can try to use action hook 'pre_get_posts' and main query.
This hook is called after the query variable object is created, but before the actual query is run.
The pre_get_posts action gives developers access to the $query object by reference (any changes you make to $query are made directly to the original object - no return value is necessary).
add_action('pre_get_posts','yourprefix_search_filter');
function yourprefix_search_filter($query) {
	if ( !is_admin() && $query->is_main_query() ) {
		if ($query->is_search) {
			//Include Custom Post Types in Search Results
			$query->set('post_type', array( 'post', 'business' ) );
			$query->set('posts_per_page', 9);
		}
	}
}

Open in new window

0
 
john_yourspaceAuthor Commented:
Thanks eemit,

I plan on having a custom search for each of my custom post types

Here is a pic of the artwork

screenshot
Search by keyword is a free text field which needs to search title and description on the custom post, the others are custom taxonomy

J
0
 
eemitCommented:
You should use the Search Parameter of the WP_Query 's'.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
john_yourspaceAuthor Commented:
I think the right way to do it is using the s parameter

http://codex.wordpress.org/Class_Reference/WP_Query#Search_Parameter
0
 
eemitCommented:
Hi john_yourspace,
two questions:
1) Did parameter s solve your problem?
2) Did you read my Comment ID: 40029265?
Quote: "You should use the Search Parameter of the WP_Query 's'."
0
 
john_yourspaceAuthor Commented:
thanks for the help
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now