La paginazione di WordPress non funziona sul template search.php

2 gen 2018, 09:32:18
Visualizzazioni: 359
Voti: 1

Ho un piccolo problema, la paginazione non funziona su search.php. Eseguo la query dei risultati con wp_Query utilizzando il codice qui sotto, riesco a vedere i link delle pagine ma quando li clicco ottengo un errore 404. Mi è stato detto che ciò accade perché eseguo la query sulla pagina search.php.

Il codice che sto usando è preso dal codice di paginazione wp, è una query lunga ma le parti importanti sono queste due righe:

$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;

e

'paged' => $paged,

query completa

if (isset($_GET['reg-prop-search-form'])){
    $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;


    $args = array(
        'posts_per_page' => 9,
        's' => $_GET['s'],
        'post_type'      => 'property',
        'paged' => $paged,
        'meta_query'     =>  array(
            'relation '  => 'AND',
            array(
                'key'       => 'prop_area',
                'value'     => $area,
                'compare'   => $area_val,
            ),
            array(
                'key'       => 'prop_city',
                'value'     => $city,
                'compare'   => $city_val,
            ),
            array(
                'key'       => 'prop_type',
                'value'     => $type,
                'compare'   => $type_val,
            ),
            array(
                'key'     => 'prop_rooms',
                'value'   => array( $fromrooms, $torooms ),
                'type'    => 'numeric',
                'compare' => 'BETWEEN',
            ),
            array(
                'key'     => 'prop_store',
                'value'   => array( $fromfloor, $tofloor ),
                'type'    => 'numeric',
                'compare' => 'BETWEEN',
            ),
            array(
                'key'     => 'prop_price',
                'value'   => array( $min, $max ),
                'type'    => 'numeric',
                'compare' => $equal,
            ),
        ),
    );  
};

questo è il loop

$the_query = new WP_Query( $args );

.....

    <?php if( $the_query->have_posts() ): ?>
        <?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>


            <?php get_template_part( 'content', 'search' ); ?>

        <?php endwhile; ?>
    <div id="pagination">

        <?php

            $big = 999999999;
             echo paginate_links( array(
                'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
                'format' => '?paged=%#%',
                'current' => max( 1, get_query_var('paged') ),
                'total' => $the_query->max_num_pages
            ) );
        ?>
        <?php echo paginate_links( $args ); ?>


    </div>
    <?php else : ?>

        <?php get_template_part( 'no-results', 'search' ); ?>

    <?php endif; ?>
    <?php wp_reset_query();  // Ripristina i dati globali del post sovrascritti da the_post(). ?>
</div>
Commenti

L'errore 404 viene generato prima che venga selezionato il tuo file template. Utilizza la query globale e l'hook pre_get_posts per manipolarla.

janh janh
2 gen 2018 09:52:01

Grazie per la tua risposta, ho letto la documentazione. Non sto utilizzando la query globale, sto usando wp_query. In quale funzione dovrei utilizzare il pre_get_post che hai menzionato?

DavSev DavSev
2 gen 2018 10:01:07

pre_get_posts è un hook globale che ti permette di utilizzare la wp_query globale, così che WP sappia quanti risultati di ricerca (e quindi: pagine di risultati) sono disponibili. Ci sono esempi che puoi adattare, ma probabilmente puoi copiare gran parte del tuo codice.

janh janh
2 gen 2018 12:53:16