WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to change search results based in content and not just titles? (6 posts)

  1. acr_project
    Member
    Posted 1 year ago #

    Hello,
    I purchased a theme for my wordpress site and it included a search form, but when you search something it doesn't include the content of the pages or posts just the title.

    So I want to change the search code so it displays results if a post has content with the keywords that the user searches.

    This is a funtion that my theme has for the search form.

    // Only display posts on search results page
    if (is_search())
    $query->set('post_type', 'post');
  2. Ctrl-C
    Member
    Posted 1 year ago #

    Post code of your search.php file on pastebin.com or similar site. Query itself doesn't affect content/title output, it basically "filters" all posts.

  3. acr_project
    Member
    Posted 1 year ago #

    Got it

    <?php
    /**
     * Search Template
     *
     * The template for displaying Search Results pages.
     */
    
    get_header(); ?>
    
    <div id="main"><div class="wrap cf">
    
    	<div id="content" role="main">
    
    	<?php get_template_part('loop-header'); ?>
    
    		<?php if (have_posts()) : global $loop_view; ?>
    
    			<?php get_template_part('loop-actions'); ?>
    
    			<div class="loop-content switchable-view <?php echo $loop_view; ?>" data-view="<?php echo $loop_view; ?>">
    				<div class="nag cf">
    					<?php while (have_posts()) : the_post();
    						get_template_part('item-video');
    					endwhile; ?>
    				</div>
    			</div><!-- end .loop-content -->
    
    			<?php get_template_part('loop-nav'); ?>
    
    		<?php else : ?>
    
    			<?php get_template_part('loop-error'); ?>
    
    		<?php endif; ?>
    
    	</div><!-- end #content -->
    
    	<?php get_sidebar(); ?>
    
    </div></div><!-- end #main -->
    
    <?php get_footer(); ?>
  4. Ctrl-C
    Member
    Posted 1 year ago #

    Post contents of item-video.php & loop-actions.php to pastebin.com, please.

  5. acr_project
    Member
    Posted 1 year ago #

    This is item-video.php

    <?php
    /**
     * The template for displaying videos in section box or on archive pages
     *
     * @package deTube
     * @subpackage Template
     * @since deTube 1.0
     */
    ?>
    	<div id="post-<?php the_ID(); ?>" <?php $item_format = is_video() ? 'video' : 'post'; post_class('item cf item-'.$item_format); ?>>
    		<?php
    			// Set image size based on section view, only for section box
    			global $section_view;
    			$thumb_size = 'custom-large';
    			if(!empty($check_view)) {
    				if($check_view == 'list-large')
    					$thumb_size = 'custom-large';
    				elseif($check_view == 'grid-mini')
    					$thumb_size = 'custom-small';
    			}
    			dp_thumb_html($thumb_size);
    		?>
    
    		<div class="data">
    			<h2 class="title"><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php printf(__('Permalink to %s', 'dp'), get_the_title()); ?>"><?php the_title(); ?></a></h2>
    
    			<p class="meta">
    				<span class="author"><?php _e('Added by', 'dp'); ?> <?php the_author_posts_link(); ?></span>
    				<span class="time"><?php printf(__('%s ago', 'dp'), human_time(get_the_time('U'))); ?></span>
    			</p>
    
    			<p class="stats"><?php echo dp_get_post_stats(); ?></p>
    
    			<p class="desc"><?php dp_excerpt(); ?></p>
    		</div>
    	</div><!-- end #post-<?php the_ID(); ?> -->

    And this is

    Loop-actions.php

    <?php
    /**
     * Loop Actions Template
     *
     * The template displays the loop actions on archive pages.
     *
     * @package deTube
     * @subpackage Template
     * @since deTube 1.0
     */
    ?>
    	<?php
    		global $wp_query;
    
    		$actived_sort_types = dp_actived_sort_types(); // Get actived sort types
    		$actived_view_types = dp_actived_view_types(); // Get actived view types
    
    		/* Get the loop view of current page based on
    		 * user's cookie and the actived sort types
    		 */
    		global $loop_view;
    		if(!empty($_COOKIE['loop_view']) && in_array($_COOKIE['loop_view'], array_keys($actived_view_types)))
    			$loop_view = $_COOKIE['loop_view'];
    		elseif(!empty($actived_view_types)) {
    			$_view_types = array_keys($actived_view_types);
    			$loop_view = $_view_types[0];
    		} else {
    			$loop_view = 'grid-small';
    		}
    
    		$loop_actions_status = get_option('dp_loop_actions_status');
    		if(!$loop_actions_status)
    			return;
    
    		// Output loop action bar if has actived sort types or view types
    		if(!empty($actived_sort_types) || !empty($actived_view_types)) {
    			echo '<div class="loop-actions cf">';	
    
    			/* Output sort(orderby and order) if has actived sort types
    			 *=======================================================*/
    			if(!empty($actived_sort_types)) {
    				// Get base url uses remove_query_arg() function
    				$base_url = remove_query_arg(array('meta_key', 'meta_value', 'orderby', 'order'));
    
    				// Get query vars from $wp_query uses get_query_var() function
    				$meta_key = get_query_var('meta_key');
    				$orderby = get_query_var('orderby');
    				$order = strtoupper(get_query_var('order'));
    
    				// Get 'order' var from url
    				$url_order = isset($_GET['order']) ? strtoupper($_GET['order']) : '';
    				if($url_order != 'ASC' && $url_order != 'DESC')
    					$url_order = '';
    
    				// Get current sort type based query vars
    				if($orderby == 'meta_value_num' && !empty($meta_key)) {
    					$current_sort_type = $meta_key;
    				} elseif($orderby == 'comment_count') {
    					$current_sort_type = 'comments';
    				} else {
    					$current_sort_type = $orderby;
    				}
    
    				// Output orderby
    				$out_orderby = array();
    				$orderby_items = array();
    				$i = 1;
    				foreach($actived_sort_types as $type => $args) {
    					// Start build url
    					$url = $base_url;
    
    					// Add order to surl if has specific correct order in current url
    					if($url_order)
    						$url = add_query_arg('order', strtolower($order), $url);
    
    					// Add sort type as orderby
    					$url = add_query_arg(array('orderby'=>$type), $url);
    
    					// Check the sort typs is current?
    					$current = '';
    					if($type == $current_sort_type || (empty($current_sort_type) && $i == 1))
    						$current = ' current';
    
    					// Output sort type link
    					$out_orderby[] = ' <a href="'.$url.'" title="'.esc_attr($args['title']).'" class="'.$type.$current.'"><i>'.$args['label'].'</i></a> ';
    
    					$orderby_items[$type] = array(
    						'url' => $url,
    						'title' => esc_attr($args['title']),
    						'label' => esc_attr($args['label']),
    						'current' => ($type == $current_sort_type) || (empty($current_sort_type) && $i == 1)
    					);
    
    					$i++;
    				}
    
    				// Output order(ASC/DESC) based user's settings
    				$out_order = '';
    				if(get_option('dp_sort_order')) {
    					// Supported order types
    					$order_types = array(
    						'ASC' => __('Ascendiente', 'dp'),
    						'DESC' => __('Descendiente', 'dp')
    					);
    
    					// Generate reversed order to url
    					$to_order = ($order == 'ASC') ? 'desc' : 'asc';
    					$to_order_url = add_query_arg('order', $to_order);
    
    					// Output order link
    					$out_order = '<span class="order"><a class="'.strtolower($order).'" href="'.$to_order_url.'" title="'.esc_attr($order_types[$order]).'">'.$order_types[$order].'</a></span><!-- end .order -->';
    				}
    
    				// Output sort html
    				echo '<div class="sort"><span class="prefix">'.__('Ordenar:', 'dp').'</span>';
    				echo '<span class="orderby">'.implode('<span class="sep">|</span>', $out_orderby).'</span><!-- end .orderby -->';
    
    				echo '<select class="orderby-select">';
    
    				foreach($orderby_items as $type => $item) {
    					echo '<option value="'.$item['url'].'"'.selected($item['current'], true, false).'>'.$item['label'].'</option>';
    				}
    				echo '</select>';
    
    				echo $out_order;
    				echo '</div><!-- end .sort -->';
    			}
    
    			/* Output view if has actived view types
    			 *=======================================================*/
    			if(!empty($actived_view_types)) {
    				echo '<div class="view"><span class="prefix">'.__('Vista:', 'dp').'</span>';
    				$i = 0;
    				foreach($actived_view_types as $type => $label) {
    					// Check the view typs is current?
    					$current = '';
    					if($type == $loop_view)
    						$current = ' current';
    
    					// Output view type link
    					echo '<a href="#" title="'.esc_attr($label).'" data-type="'.$type.'" class="'.$type.'-link'.$current.'"><i></i></a>';
    				}
    
    				echo '</div><!-- end .view -->';
    			}
    
    			echo '</div><!-- end .loop-actions -->';
    		}
    	?>

    Thanks

  6. acr_project
    Member
    Posted 1 year ago #

    I found this plug in called WP Search that basically searches the content and displays the results

Topic Closed

This topic has been closed to new replies.

About this Topic