Search Custom Post Type and Custom Fields

Posted in: Newsmag
Post count: 31

Hi,

I have an event post type. In addition, I have custom fields. I would like to search these fields.

Normally, this would be ok but unsure how to do it on Newsmag. How do I amend the search facility for a specific post type so it filters what people choose? This is what I have so far but I would like to expand the current search facility

Functions.php

 function template_chooser($template)   
{    
  global $wp_query;   
  $post_type = get_query_var('post_type');   
  if( $wp_query->is_search && $post_type == 'event' )   
  {
    return locate_template('event-search.php');  //  redirect to archive-search.php
  }   
  return $template;   
}
add_filter('template_include', 'template_chooser');
add_filter( 'posts_where', 'wpse18703_posts_where', 10, 2 );
function wpse18703_posts_where( $where, &$wp_query )
{
    global $wpdb;
    if ( $wpse18703_title = $wp_query->get( 'wpse18703_title' ) ) {
        $where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'' . $wpdb->esc_like( $wpse18703_title )  . '%\'';
    }
    return $where;
}

Event-search.php

<?php
        /* Template Name: Custom Search */        
        get_header(); ?>             
        <?php 

// args
$args = array(
	'numberposts'	=> -1,
	'wpse18703_title'	=> 'reading festival',
	'post_type'		=> 'event'
);

// query
$the_query = new WP_Query( $args );
//echo "Last SQL-Query: {$the_query->request}";
?>
<?php if( $the_query->have_posts() ): ?>
	<ul>
	<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
		<li>
			<a>">
				<?php the_title(); ?>
			</a>
		</li>
	<?php endwhile; ?>
	</ul>
<?php endif; ?>

<?php wp_reset_query();	 // Restore global post data stomped by the_post(). ?>  
        <?php get_sidebar(); ?>
        <?php get_footer(); ?>

I look within loop.php but nothing there to help unfortunately. Basically – how do I add a filter to the search based on a URL parameter?

Post count: 6535

Hi

The code which generate the live search is located in td_data_source.php here: http://screencast.com/t/f8QmhDHo For the normal search page the theme use the default WordPress function: http://screencast.com/t/0WbG9C1ynaR
I am not sure how do you want to customize the search function but I assume that if the user navigate to Custom Search page template and perform a search, the posts displayed to be from events post type. This could be achieved by doing a custom search function which display posts only from event post type, then call this function in the file which generate the post type template.
Here found some informations whioch may be useful in this case: http://wordpress.stackexchange.com/questions/89886/how-to-create-a-custom-search-for-custom-post-type You can try the solution indicated in that topic and adjust it on Newsmag theme.

Please consider that this is a complex task which require many customizations and also good coding skills in order to be achieved. If you’re not aware of the steps you need to take in this regard please consider hiring a developer or a freelancer to do this for you as we can’t offer customization services at the moment.

Thanks!

Viewing 2 posts - 1 through 2 (of 2 total)
The forum ‘Newsmag’ is closed to new topics and replies.