Solved

WP Query

Posted on 2014-04-28
6
158 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
  • 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

WordPress can be pretty daunting, especially for a beginner, so I thought it might be a good idea to write an article to show how easy it is to get started in WordPress and to design a custom theme.  The first step is to check with your hosting comp…
WordPress is constantly evolving, and with each evolution appears to get better and better.  One of the big drawbacks prior to version 3 was that there was no way to be able to set up a custom menu from the backend. The Old Way Adding menus is…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

705 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now