简体   繁体   中英

WordPress custom search page is not working

I am creating a new WordPress website. Trying to create custom search page but when I am going to search anything, its not showing any result. But The query still have posts.

$s=get_search_query();
     $args = array(
      's' =>$s
     );
// The Query
$the_query = new WP_Query( $args ); // Just tested the search query

get_header();
?>

<div class="main_content">
    <div class="row">
        <div class="col-sm-12">
            <div class="custom-breadcrumb">
                <h1>News Digest &horbar; <?php printf( __( '%s', 'decouvr' ), get_search_query() ); ?></h1>
                <?php
                schema_breadcrumb();
                ?>
            </div>
        </div>
    </div>
</div>

<div class="container container-most">

    <div class="row">

        <?php

        if ( $the_query->have_posts() ) :
            // Start the loop.
            while ( $the_query->have_posts() ) : the_post();
                get_template_part( 'content', get_post_format() );

                // End the loop.
            endwhile;

            // Previous/next page navigation.
            docuvr_paging_nav();
        // If no content, include the "No posts found" template.
        else :
            get_template_part( 'content', 'none' );
        endif;
        ?>

    </div>

</div>

<?php
get_footer();
?>

I need to redirect the search page when user trying to search. I have used a function to redirect the search page

function search_url_rewrite () {
    if ( is_search() && !empty( $_GET['s'] ) ) {
        wp_redirect( home_url( '/news/' ) . urlencode( get_query_var( 's' ) ) );
        exit();
    }
}

add_action( 'template_redirect', ' search_url_rewrite ' );

Its still not working. I have tried many ways to do this. I am just stacked here. There is one thing also when I use the default search page its working very nice. Mostly I need the redirect too. Is There any wrong I am doing or is there any other way to do this?

Change while ( $the_query->have_posts() ) : the_post(); on while ( $the_query->have_posts() ) : $the_query->the_post(); you missed $the_query-> in the_post();

Also check that this path get_template_part( 'content', get_post_format() ); is correct

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM