?
Solved

WP Query

Posted on 2014-04-28
6
Medium Priority
?
179 Views
Last Modified: 2014-05-20
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
Comment
Question by:john_yourspace
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 15

Expert Comment

by:eemit
ID: 40029121
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
 

Author Comment

by:john_yourspace
ID: 40029144
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
 
LVL 15

Expert Comment

by:eemit
ID: 40029265
You should use the Search Parameter of the WP_Query 's'.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Accepted Solution

by:
john_yourspace earned 0 total points
ID: 40029351
I think the right way to do it is using the s parameter

http://codex.wordpress.org/Class_Reference/WP_Query#Search_Parameter
0
 
LVL 15

Expert Comment

by:eemit
ID: 40069473
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
 

Author Closing Comment

by:john_yourspace
ID: 40077108
thanks for the help
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question