• Resolved alexbourlier

    (@alexbourlier)


    Hi,

    This is how I override the WordPress search page to implement better results thanks to Relevanssi:

    
    global $wp_query;
            $search_txt = get_query_var('s');    
            $args = array(
              's' => $search_txt,
              'post_type' => 'formations',
              'posts_per_page' => 9,
              'paged' => $wp_query->query_vars['paged'], // conserver le numéro de page de la requête initiale
            );
    
            // filtrer suivant la bonne taxonomy
            if (isset($_GET['taxonomy'])) {
              switch ($_GET['taxonomy']) {
                case 'formation-diplomantes-cpf':
                  $ta = ['formation-diplomante', 'formation-eligible-au-cpf'];
                  $op = 'AND';
                break;
    
                case 'toute-formation':
                break;
    
                default:
                  $ta = $_GET['taxonomy'];
                  $op = 'IN';
              }
    
              if (isset($ta)) {
                $tq = [[
                  'taxonomy' => 'type_form',
                  'field'    => 'slug',
                  'terms'    => $ta,
                  'operator' => $op,
                ]];// Tax Query
    
                $args['tax_query'] = $tq;
              }
            }
    
            $fq = new WP_Query();
            $fq->parse_query( $args );
    
            relevanssi_do_query($fq);
    
            $any_formation  = false;
            $fdia           = [];// Formations DIOGEN IDs Array
            $fia            = [];// Formations IDs Array
            $i=0;
            while ($fq->have_posts()) : $fq->the_post();
              if ( 'formations' == get_post_type() ) { 
                $i++;
                $fdia[get_the_ID()]     = get_field('id_diogen', get_the_ID());
                $fia[]                  = get_the_ID();
                $any_formation      = true;
              }
            endwhile;
            ?>
    

    The results are paginated:

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

    This works well in most cases:
    https://cdma.happy-dev.fr/?taxonomy=toute-formation&s=design
    https://cdma.happy-dev.fr/page/2/?taxonomy=toute-formation&s=design
    https://cdma.happy-dev.fr/page/3/?taxonomy=toute-formation&s=design

    It fails in others:
    https://cdma.happy-dev.fr/?taxonomy=toute-formation&s=sophie
    https://cdma.happy-dev.fr/page/2/?taxonomy=toute-formation&s=sophie
    https://cdma.happy-dev.fr/page/3/?taxonomy=toute-formation&s=sophie

    I ended up understanding that pagination of my formations was not working when I didn’t have enough actualites. That is, say I have a query that brings 10 formations, and 20 actualites: everything works as expected. The opposite fails however.

    This is how I rewrite URLs regarding actualites:

    
    function custom_rewrite_rules( $wp_rewrite ) {
      $wp_rewrite->rules = array(
        'actualite/page/?([0-9]{1,})/?$' => $wp_rewrite->index . '?pagename=actualite&paged=' . $wp_rewrite->preg_index( 1 ),
    
      ) + $wp_rewrite->rules;
    }
    add_action( 'generate_rewrite_rules', 'custom_rewrite_rules' );
    

    I tried to had a prefix to the search URL thinking that could play a role, but to no avail.
    I also tried adding a custom rewrite rule for the search page, but it is not having any impact. I still get 404s.
    I spent hours digging every possible direction but to no avail.

    Any suggestion is most welcome.
    I don’t see why both pagination of formations and actualites are related.

    • This topic was modified 4 years, 8 months ago by alexbourlier.
    • This topic was modified 4 years, 8 months ago by alexbourlier.
    • This topic was modified 4 years, 8 months ago by alexbourlier.
    • This topic was modified 4 years, 8 months ago by alexbourlier.

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Mikko Saari

    (@msaari)

    On the page two of the sophie query, what parameters the relevanssi_do_query() gets and does it return any posts?

    What’s in the $wp_query->query_vars on the broken page load?

    How are the actualites fetched?

    Thread Starter alexbourlier

    (@alexbourlier)

    Thank you for your response Mikko.

    The thing is that the code bypasses the search.php file on page 2 and gets straight to 404 response. relevanssi_do_query() is not called on that page then.

    $wp_query->query_vars is worth the following on the 404 page:

    
    => 0 [monthnum] => 0 [year] => 0 [w] => 0 [category_name] => [tag] => [cat] => [tag_id] => [author] => [author_name] => [feed] => [tb] => [meta_key] => [meta_value] => [preview] => [sentence] => [title] => [fields] => [menu_order] => [embed] => [category__in] => Array ( ) [category__not_in] => Array ( ) [category__and] => Array ( ) [post__in] => Array ( ) [post__not_in] => Array ( ) [post_name__in] => Array ( ) [tag__in] => Array ( ) [tag__not_in] => Array ( ) [tag__and] => Array ( ) [tag_slug__in] => Array ( ) [tag_slug__and] => Array ( ) [post_parent__in] => Array ( ) [post_parent__not_in] => Array ( ) [author__in] => Array ( ) [author__not_in] => Array ( ) [ignore_sticky_posts] => [suppress_filters] => [cache_results] => 1 [update_post_term_cache] => 1 [lazy_load_term_meta] => 1 [update_post_meta_cache] => 1 [post_type] => any [posts_per_page] => 9 [nopaging] => [comments_per_page] => 50 [no_found_rows] => [search_terms_count] => 1 [search_terms] => Array ( [0] => sophie ) [search_orderby_title] => Array ( [0] => gretacdma_posts.post_title LIKE '{889f9dde2793b886b91eb47378897f22eb2098e471145dec5d64e49020558861}sophie{889f9dde2793b886b91eb47378897f22eb2098e471145dec5d64e49020558861}' ) [order] => DESC [operator] => OR ) 
    

    On search.php, after displaying the formations, actualites are fetched this way:

    
          $pq         = new WP_Query('s='.$search_txt);// Posts Query                                        
          $pq->query_vars['post_type']        = 'post';                                                      
          $pq->query_vars['posts_per_page']   = 9999;                                                        
          $pq->query_vars['orderby']          = 'date';                                                      
          relevanssi_do_query($pq);
    

    If easier, I can give you access to the code repo with your email address.

    Plugin Author Mikko Saari

    (@msaari)

    I think part of the problem is the fact you’re doing the query on the search results template. If the default query doesn’t find any results, as you can see the search doesn’t even go to the search results template.

    So, instead of doing the query on the search results template, you should do it before the template by adjusting the parameters for the default query. Use pre_get_posts or relevanssi_modify_wp_query to change the default query to change the post type and other parameters and use that default query (and then fetch the news with another query).

    This way the default query will match what you display on the page, and the paging will work.

    Thread Starter alexbourlier

    (@alexbourlier)

    I understand better now. I was kind of doing it like that (via pre_get_post) but was facing that pagination issue, and searching for an answer I found a post of you saying having a query_posts in the pre_get_posts hook would screw things up, so I back pedaled from there and moved everything in search.php. I might have gotten it wrong though.

    I try your suggestion out and reach back. Thanks for your help!

    Thread Starter alexbourlier

    (@alexbourlier)

    Hmmm…

    This is how I implement relevanssi_modify_wp_query in function.php

    
    function modify_search($wp_query) {
      $wp_query->set('s', get_query_var('s'));// fetch search string
      $wp_query->set('post_type', 'formations');
      $wp_query->set('posts_per_page', 9);
      $wp_query->set('paged', $wp_query->query_vars['paged']);// paging
    
      // filtrer suivant la bonne taxonomy
      if (isset($_GET['taxonomy'])) {
        switch ($_GET['taxonomy']) {
          case 'formation-diplomantes-cpf':
            $ta = ['formation-diplomante', 'formation-eligible-au-cpf'];
            $op = 'AND';
          break;
    
          case 'toute-formation':
          break;
    
          default:
            $ta = $_GET['taxonomy'];
            $op = 'IN';
        }
    
        if (isset($ta)) {
          $tq = [[
            'taxonomy' => 'type_form',
            'field'    => 'slug',
            'terms'    => $ta,
            'operator' => $op,
          ]];// Tax Query
    
          $wp_query->set('tax_query', $tq);
        }
      }
    
      return $wp_query;
    }
    add_filter('relevanssi_modify_wp_query', 'modify_search');
    

    I get no result: https://cdma.happy-dev.fr/?s=design&taxonomy=toute-formation

    Even though $wp_query->query_vars seem correct to me. I’m echoing them from search.php:

    
    global $wp_query;
    print_r($wp_query->query_vars);
    

    Output:

    
    Array ( [s] => design [taxonomy] => toute-formation [error] => [m] => [p] => 0 [post_parent] => [subpost] => [subpost_id] => [attachment] => [attachment_id] => 0 [name] => [pagename] => [page_id] => 0 [second] => [minute] => [hour] => [day] => 0 [monthnum] => 0 [year] => 0 [w] => 0 [category_name] => [tag] => [cat] => [tag_id] => [author] => [author_name] => [feed] => [tb] => [paged] => 0 [meta_key] => [meta_value] => [preview] => [sentence] => [title] => [fields] => [menu_order] => [embed] => [category__in] => Array ( ) [category__not_in] => Array ( ) [category__and] => Array ( ) [post__in] => Array ( ) [post__not_in] => Array ( ) [post_name__in] => Array ( ) [tag__in] => Array ( ) [tag__not_in] => Array ( ) [tag__and] => Array ( ) [tag_slug__in] => Array ( ) [tag_slug__and] => Array ( ) [post_parent__in] => Array ( ) [post_parent__not_in] => Array ( ) [author__in] => Array ( ) [author__not_in] => Array ( ) [tax_query] => Array ( [0] => Array ( [taxonomy] => language [field] => term_taxonomy_id [terms] => 622 [operator] => IN ) [1] => Array ( [taxonomy] => language [field] => term_taxonomy_id [terms] => 622 [operator] => IN ) ) [ignore_sticky_posts] => [suppress_filters] => [cache_results] => 1 [update_post_term_cache] => 1 [lazy_load_term_meta] => 1 [update_post_meta_cache] => 1 [post_type] => formations [posts_per_page] => 9 [nopaging] => [comments_per_page] => 50 [no_found_rows] => [search_terms_count] => 1 [search_terms] => Array ( [0] => design ) [search_orderby_title] => Array ( [0] => gretacdma_posts.post_title LIKE '{cb793a21e7786cd73b23061f81ecde61c38e2f18a4e56af2c57586031d8c210b}design{cb793a21e7786cd73b23061f81ecde61c38e2f18a4e56af2c57586031d8c210b}' ) [order] => DESC [operator] => OR ) 
    
    Thread Starter alexbourlier

    (@alexbourlier)

    Arf… The polylang plugin was actually the cause… Deactivating it solved the problem.

    Many thanks for your helped. Without your help, I would still be on it. Thanks!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Page not found on some search pages on my search result’ is closed to new replies.