<?php
$search = getQuerySingleParam('search'); // to co uzytkownik wpisal w pole search
?>
----------------------------------Formularz------------------------------------------------------------
<form class="search" method="get" action="">
<label for="search">Znajdź Wpis:</label>
<fieldset>
<input type="text" name="search" id="search" value="<?php echo $search ?>" />
<input type="submit" value="Szukaj" />
</fieldset>
</form>
----------------------------------Zawezenie petli------------------------------------------------------
<?php
$query_params = getQueryParams(); // zawezenie petli z wynikami
if(isset($query_params['search'])){
$query_params['post_title_like'] = $query_params['search'];
unset($query_params['search']);
}
$loop = new WP_Query($query_params);
?>
----------------------------------Wypisanie postow-----------------------------------------------------
<?php
if($loop->have_posts()):
?>
<?php
while($loop -> have_posts()) : $loop -> the_post();
?>
<a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>
<p><?php the_content(); ?></p>
<?php endwhile; ?>
<?php else: ?>
<h1>Nie znaleziono odpowiednich wpisow</h1>
<?php endif; ?>
----------------------------------Potrzebne funkcje----------------------------------------------------
<?php
function getQueryParams(){ // wyciaganie wartosci z query_string
global $query_string;
$parts = explode('&', $query_string);
$return = array();
foreach($parts as $part){
$tmp = explode('=', $part);
$return[$tmp[0]] = trim(urldecode($tmp[1]));
}
return $return;
}
function getQuerySingleParam($name){
$qparams = getQueryParams();
if(isset($qparams[$name])){
return $qparams[$name];
}
return NULL;
}
add_filter('posts_where', 'title_like_posts_where', 10, 2); // wyszukiwarka
function title_like_posts_where( $where ) {
global $wpdb;
global $query_params;
if ($post_title_like = $query_params['post_title_like']){
$where .= ' AND '. $wpdb->posts . '.post_title LIKE \'%' . $wpdb->esc_like($post_title_like) . '%\'';
//$where .= ' AND '. $wpdb->posts . '.comment_count > 2';
}
//print_r($where);
return $where;
}
?>
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.