• The following code is the code of my search.php and if the search has no result (no posts found) i want it to redirect to another page like https://www.example.com… What i must change?

    <?php get_header() ?>
    
    <div class="sleeve_main">
    
    	<div id="main">
    		<h2><?php printf( __( 'Search Results for: %s', 'p2' ), esc_html( get_search_query() ) ); ?></h2>
    
    		<?php if ( have_posts() ) : ?>
    
    			<ul id="postlist">
    			<?php while ( have_posts() ) : the_post() ?>
    
    				<?php p2_load_entry() // loads entry.php ?>
    
    			<?php endwhile; ?>
    			</ul>
    
    		<?php else : ?>
    
    			<div class="no-posts">
    			    <h3><?php _e( 'No posts found!', 'p2' ) ?></h3>
    
    			</div>
    
    		<?php endif ?>
    
    		<div class="navigation">
    			<p><?php posts_nav_link( ' | ', __( '&larr;&nbsp;Newer&nbsp;Posts', 'p2' ), __( 'Older&nbsp;Posts&nbsp;&rarr;', 'p2' ) ); ?></p>
    		</div>
    
    	</div> <!-- main -->
    
    </div> <!-- sleeve -->
    
    <?php get_footer() ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • You can use the wp_redirect function. Something like this should work (I didn’t test it):

    <?php else : ?>
             <?php wp_redirect('https://www.example.com'); ?>
    <?php endif ?>

    Thread Starter ideecon

    (@ideecon)

    no it doesnt work….

    Warning: Cannot modify header information - headers already sent by (output started at /www/

    Delete..

    // Ignore this line - no indentation on first line workaround (forum bug)
    		<?php else : ?>
    
    			<div class="no-posts">
    			    <h3><?php _e( 'No posts found!', 'p2' ) ?></h3>
    
    			</div>

    Then place this at the very top of the file..

    <?php if( !have_posts() ) wp_redirect( 'https://example.com' ); ?>

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Redirect Search Result’ is closed to new replies.