Solved

WP Query

Posted on 2014-04-28
6
175 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 

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

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!

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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!
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 purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…

734 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