W
W
wqexxx2022-04-16 11:21:26
WordPress
wqexxx, 2022-04-16 11:21:26

How to display custom post search results on a custom page?

Hello, please tell me how to display the search results of custom post types (not in search.php, as the results of regular posts are displayed there) in search-blog.php using a form?

<form class="search-from-wp" role="search" method="get" id="searchform" action="<?php echo home_url( '/' ) ?>">
            <input class="search-field-wp" type="text"  placeholder="Поиск" value="<?php echo get_search_query(); ?>" name="b" id="b" />
           <span class="search-submit-wp-wrap">
                <button class="search-submit-wp" type="submit" id="searchsubmit" value=""><i class="icon-search-wp fa fa-search"> 
               </i></button>
          </span>
</form>

Thanks

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Litvinenko, 2022-04-16
@AntonLitvinenko

sort of like this

<?php while ( have_posts() ) : the_post(); ?>
<li>
    <?php
    if ($post->post_type == "cards") {
       //do fancy things


    }
    if ($post->post_type == "mycards") {
       //do fancy things


    }
    else {
       //otherwise just sit
    }

    <article>
        <h2><a href="<?php esc_url( the_permalink() ); ?>" title="Permalink to <?php the_title(); ?>" rel="bookmark"><?php the_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(); ?>
    </article>
    ?>

</li>

A
Artem Zolin, 2022-04-19
@artzolin

I will supplement the answer of Anton Litvinenko . If you need to sort posts by type, this can be done on the filterpre_get_posts

add_action( 'pre_get_posts', 'pre_search_filter' );
function pre_search_filter( $query ){

  if ( !is_admin() && $query->is_main_query() && $query->is_search ) {
    $query->set( 'orderby', 'type' );
  }
  
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question