• giovanniganzinotti

    (@giovanniganzinotti)


    I am using WordPress 4.3.1. Today I tried to add search functionality to my webpage (https://ganzinotti.com/aisb/). I created a searchform.php and a search.php. The search functionality works.

    The only problem is that my ‘secondary’ loops are broken on my page displaying the search results. All my ‘secondary’ loops are initiated with “new WP_Query()”. I feel there is nothing wrong with my code, because on all other pages these additional loops are rendered correctly. Only on the search page there seems to be something wrong.

    If you visit https://ganzinotti.com/aisb/ and scroll down to the footer you can see how it should be rendered. If you do a search for a keyword that has no results, nothing is rendered: https://ganzinotti.com/aisb/?s=abcd. If you search for a keyword with multiple hits it only renders the actual posts with hits: https://ganzinotti.com/aisb/?s=ethics.

    I did find multiple people on the web with the same problem, but I didn’t find any solutions yet. It tried resetting the main loop and secondary loops in all possible combinations with “wp_reset_postdata();” and “wp_reset_query();”, but this didn’t help. According to my observations the global wp_query in the search page is stripped and reduced to only those posts that are a hit. Is there a way to change this and keep the wp_query with all posts, so I can run my secondary loops and maybe specify a custom “new WP_Query()” with the search string as argument…

    Any help is more than welcome.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Michael

    (@alchymyth)

    please post the full code of search.php

    Thread Starter giovanniganzinotti

    (@giovanniganzinotti)

    The search.php:

    <?php get_header(); ?>
    <div id=”page”>
    <?php get_sidebar(); ?>
    <div id=”content” role=”main”>
    <?php

    if ( have_posts() ) : ?>

    <div class=”content-main”>
    <h1 class=”title”><?php printf( __( ‘Search Results for: %s’, ‘bolivia’ ), ‘<span>’ . get_search_query() . ‘</span>’ ); ?></h1>
    <p><?php printf( __( ‘Found %s Results.’, ‘bolivia’ ), $wp_query->found_posts ); ?></p>
    </div>

    <?php while ( have_posts() ) : the_post();
    if ( ‘video’ == get_post_type() ) {
    get_template_part( ‘loop-video’ );
    }
    else {
    get_template_part( ‘loop-summary’ );
    }
    endwhile;
    else : ?>
    <div class=”content-main”>
    <h1 class=”title”>Nothing Found</h1>
    <p>Sorry, no search results. Please try again with some different keywords.</p>
    <?php get_search_form(); ?>
    </div>
    <?php endif; ?>
    </div>
    </div>
    </div>
    <?php get_footer(); ?>

    I tried many different search.php files, but somehow I feel that there should be no reason why the search.php breaks my ‘secondary’ loops in the footer and the recent activity tab.

    Michael

    (@alchymyth)

    breaks my ‘secondary’ loops in the footer

    what is the full code of those secondary loops? or the full code of footer.php?

    Thread Starter giovanniganzinotti

    (@giovanniganzinotti)

    I have three ‘secondary’ loops in my footer:
    <div id=”footer-latest-books”>
    <h5 class=”footer-white”>Books</h5>
    <?php
    $query_footer_books = new WP_Query( array(
    ‘post_type’ => ‘books’,
    ‘orderby’ => ‘rand’,
    ‘posts_per_page’ => 4,
    ‘tax_query’ => array(
    array(
    ‘taxonomy’ => ‘cat_books’,
    ‘field’ => ‘id’,
    ‘terms’ => array( 23, 26 )
    ),
    ),
    )
    );

    while ( $query_footer_books->have_posts() ) : $query_footer_books->the_post(); ?>
    ” title=”<?php the_title(); ?>”><?php the_post_thumbnail(‘book-thumbnail’); ?>
    <?php endwhile;
    wp_reset_postdata();
    ?>
    </div>

    <div id=”footer-latest-movies”>
    <h5 class=”footer-white”>Movies</h5>
    <?php
    $query_footer_movies = new WP_Query( array(
    ‘post_type’ => ‘movies’,
    ‘orderby’ => ‘rand’,
    ‘posts_per_page’ => 4
    ));

    while ( $query_footer_movies->have_posts() ) : $query_footer_movies->the_post(); ?>
    ” title=”<?php the_title(); ?>”><?php the_post_thumbnail(‘movie-thumbnail’); ?>
    <?php endwhile;
    wp_reset_postdata();
    ?>
    </div>

    <div id=”footer-video”>
    <h5 class=”footer”><?php _e(‘Videos’, ‘bolivia’); ?></h5>
    <?php
    $args = array(
    ‘post_type’ => ‘video’,
    ‘orderby’ => ‘rand’,
    ‘posts_per_page’ => 1
    );
    $random_video_posts_query = new WP_Query( $args );
    while ( $random_video_posts_query->have_posts() ) : $random_video_posts_query->the_post();
    echo do_shortcode( ‘[youtube=’ . get_post_meta( $post->ID, ‘_cmb_youtube_url’, true ) . ‘&w=195&h=120]’ );
    endwhile;

    wp_reset_postdata();
    ?>
    </div>

    Michael

    (@alchymyth)

    I could not reproduce the behaviour in a test setup (but I did not use custom post types).

    generally, in footer.php, before your code, try to add:

    <?php $temp = $wp_query;
    $wp_query = null; ?>

    and afer your code, add:

    <?php $wp_query = $temp; ?>
    Thread Starter giovanniganzinotti

    (@giovanniganzinotti)

    This breaks my code:

    Books
    Fatal error: Call to a member function get_queried_object_id() on null in /hermes/bosweb25a/b504/ipg.ganzinotticom/wp-includes/query.php on line 57

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘All secondary loops on search page are broken!’ is closed to new replies.