Skip to content

Instantly share code, notes, and snippets.

@Dziuperman
Last active July 5, 2019 04:36
Show Gist options
  • Save Dziuperman/25d474b5e5bb6ebdbf454f58d0021108 to your computer and use it in GitHub Desktop.
Save Dziuperman/25d474b5e5bb6ebdbf454f58d0021108 to your computer and use it in GitHub Desktop.
Search by posts on the search page
<?php
$args = array(
'post_type' => 'page',
'name__like' => $_GET['s'],
);
$query = new WP_Query( $args ); ?>
<?php if ( have_posts() ): ?>
<h2>Search Results for '<?php echo get_search_query(); ?>'</h2>
<ol>
<?php while (have_posts() ) : the_post(); ?>
<li>
<h2><a href="<?php esc_url( the_permalink() ); ?>" title="Permalink to <?php the_title(); ?>" rel="bookmark"><?php echo $post->post_title; ?></a></h2>
<time datetime="<?php the_time( 'Y-m-d' ); ?>" pubdate><?php the_date(); ?> <?php the_time(); ?></time> <?php comments_popup_link('Leave a Comment', '1 Comment', '% Comments'); ?>
<?php the_content(); ?>
</li>
<?php endwhile; ?>
</ol>
<?php else: ?>
<h2>No results found for '<?php echo get_search_query(); ?>'</h2>
<?php endif; ?>
<?php
$posts = get_posts( array(
'post_type' => 'page',
'category' => 0,
'order' => 'DESC',
'meta_key' => '',
'meta_value' =>'',
's' => $_GET['s'],
'tax_query' => array(
)
) );
if ( $posts ):
foreach( $posts as $post ):
setup_postdata($post);
// формат вывода the_title() ...
if($post->post_type == 'page'): ?>
<li>
<a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( $post->post_title ) ?> download">
<span>Страница</span>
<?php echo esc_html( $post->post_title ) ?>
</a>
</li>
<?php
elseif($post->post_type == 'documents'):
$document_id = get_post_meta($post->ID, 'documents_file', true);
$document_url = wp_get_attachment_url( $document_id );
$document_pathinfo = pathinfo( get_attached_file($document_id));
$document_extension = $document_pathinfo['extension'];
?>
<li>
<a href="<?php echo $document_url; ?>" title="<?php echo esc_attr( $post->post_title ) ?>">
<?php echo esc_html( $post->post_title ) ?>
<span>(<?php echo $document_extension; ?>)</span>
<span>Скачать</span>
<?php the_time( 'Y.m.d' ); ?>
</a>
</li>
<?php
endif;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment