Link to home
Start Free TrialLog in
Avatar of john_yourspace
john_yourspace

asked on

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

Avatar of eemit
eemit
Flag of Germany image

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

Avatar of john_yourspace
john_yourspace

ASKER

Thanks eemit,

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

Here is a pic of the artwork

User generated image
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
You should use the Search Parameter of the WP_Query 's'.
ASKER CERTIFIED SOLUTION
Avatar of john_yourspace
john_yourspace

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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'."
thanks for the help