Search results counted but not displayed properly – WordPress

I have a problem with my search.php file. What I want to achieve is that when two of my custom post types are included in the search results, the user gets a link to other (“parent”) page. What I have achieved by now is that the search results are being properly counted, but results other than those custom post types are not being displayed properly (page title and URL do not appear). Here is my code (search.php).

<?php
/*
Template Name: Search results
The template for displaying Search Results pages.
*/
get_header();
?>
<?php
global $query_string;
wp_parse_str( $query_string, $search_query );
$search = new WP_Query( $search_query );
?>
<div class="container-fluid-wrapper">
<div class="container-fluid przewodnik bottom przewodnik-obszary-wrapper">
    <div class="row no-gutters przewodnik-wstep">
    <div class="col-xl-1 jasne-tlo">
    </div> 
    <div class="col-xl-10 jasne-tlo border-left-right no-background bottom-space search-wrapper">
        <div class="search">
            <h3 class="krok krok-przewodnik">Wyniki dla:</h3>
                <?php /* Search Count */ $allsearch = new WP_Query("s=$s&showposts=-1"); $key = wp_specialchars($s, 1); $count = $allsearch->post_count; _e(''); _e('<span class="search-terms">'); echo $key; _e('</span>'); _e(' <br> '); _e('<span class="search-number">'); _e('Liczba wyników: '); _e('</span">'); _e('<span class="bold">'); echo $count . ' '; _e('</span>'); wp_reset_query(); ?> <br>
        </div>
    <?php if ($allsearch->have_posts()) : ?>
    <?php while ($allsearch->have_posts()) : $allsearch->the_post(); ?>
        <?php
            if ($post->post_type == "zespol") {
                echo '<div class="row">
                        <div class="col-xl-5 search-result pole-zainteresowac no-background-image">
                            <a class="no-color" href="./o-nas/"> O nas </a>
                        </div>
                    </div>';        
            }
            else if ($post->post_type == "co_nowego") {
                echo '<div class="row">
                        <div class="col-xl-5 search-result pole-zainteresowac no-background-image">
                            <a class="no-color" href="./co-nowego/"> Co nowego </a>
                        </div>
                    </div>';
            }  else {
                echo '<div class="row">
                        <div class="col-xl-5 search-result pole-zainteresowac no-background-image">
                            <a class="no-color" href="<?php echo esc_url(get_permalink()); ?>"><?php the_title(); ?> </a>   
                        </div>
                    </div>';        
                } ?>
    <?php endwhile; else: ?>
<!-- 404 SEARCH -->
    <div class="404-search">
        <?php _e("Oops... We couldn't find what you were searching for. Please try again"); ?>
            <div class="row">
                <div class="col-xl-6 search-form-2">
                    <?php get_search_form(); ?> 
                </div>
                <div class="col-xl-1 szukaj-label">
                    <p>Szukaj</p>
                </div>
            </div>
    </div>
<!-- / 404 SEARCH -->

        <?php endif; ?>
    </div> <!-- end "col-xl-10 border-left-right" -->
    <div class="col-xl-1 jasne-tlo right">
        <img class="strzalka-dol-przewodnik" src="<?php echo get_stylesheet_directory_uri(); ?>/img/STRZALKA_DOL-04_cropped_long.svg" />
        </div>
    </div>  <!-- END "row przewodnik-wstep" -->
    </div> <!-- END "container przewodnik" -->
    </div> <!-- END "container-fluid-wrapper -->
    <?php get_template_part( 'template-parts/footer-menus-widgets' ); ?>
    <?php
    get_footer();

When I don’t try to redirect those custom post types (when I don’t use if statement with post types), search results are being displayed properly.

Answer

$post is probably not defined here for your post type checks… change your $post->post_type == "zespol" if checks to:

if ( get_post_type() === "zespol" ) {
    // do something...
}else if ( get_post_type() === "co_nowego" ) {
    // do something else...
}else {
    // default to this...
}

UPDATE

you need to fix your echo statement for the last else case to use concatenation like below:

echo '<div class="row">
    <div class="col-xl-5 search-result pole-zainteresowac no-background-image">
        <a class="no-color" href='. esc_url(get_permalink()) .'">'. get_the_title() .'</a>   
    </div>
</div>';

Leave a Reply

Your email address will not be published. Required fields are marked *