0

I have this url:

mydomain.com/?s="SEARCH TERM"&categoria="WORDPRESS CATEGORY OF POSTS"&province="CUSTOM POST FIELD"&poblacion="CUSTOM POST FIELD"

How can I make that URL friendly with each of the fields?

is a custom Wordpress search form, with 4 fields.

Province is a POST custom field, custom_field.

The URL I would like to have is: midominio.com/categoria-en-poblacion-provincia/termino-de-busqueda.html

add_action( 'init',  function() {
  add_rewrite_rule(
     '(.+)-en-(.+)-(.+)/(.+).html?$',
       'index.php?s=$matches[1]&categoria=$matches[2]&provincia=$matches[3]&poblacion=$matches[4]',
       'top'
   );
} );

add_filter( 'query_vars', function( $query_vars ) {
  $query_vars[] = 'categoria';
  $query_vars[] = 'poblacion';
  $query_vars[] = 'provincia';
  $query_vars[] = 's';
  return $query_vars;
} );


add_action('template_redirect', 'change_search_url_rewrite');
function change_search_url_rewrite()
{
  if (is_search()) {
        $search_url = get_bloginfo('url') .'/'. get_query_var('categoria') . '-en-' . get_query_var('poblacion') . '-' . get_query_var('provincia') . '/' . get_query_var('s') . '.html';
        if (!empty($_GET['s']) && !empty($_GET['categoria']) && !empty($_GET['provincia'])  && !empty($_GET['poblacion']))
            wp_redirect($search_url);
    }
  if (is_search()) {
    $search_url = get_bloginfo('url') . '/' . get_query_var('categoria') . '/';
    if (empty($_GET['s']) && !empty($_GET['categoria']) && empty($_GET['provincia']) && empty($_GET['poblacion']))
      wp_redirect($search_url);
  }
 
}


This redirects me, but doesn't show the results...

<form method="GET" action="index.php" role="search" id="searchform">

<input type="text" id="s" name="s">


<select class="form-control control azul" name="categoria" id="categoria">
<OPTION value="">Seleccione categoría</option>
<OPTION value="">Coches</option>
<OPTION value="">Motos</option>
</select>


<select name="provincia" id="provincia" onchange="">
<OPTION value="">Seleccione provincia</option>
<OPTION value="">Alicante</option>
<OPTION value="">Barcelona</option>
</select>

<select id="Poblacion" name="poblacion">
<option value="" selected>Selecciona primero una provincia...</option>
<OPTION value="">Torrevieja</option>
<OPTION value="">Orihuela</option>
</select>

<button type="submit" value="Buscar" class="btn btn-primary" title="Buscar"><i class="fa fa-search" aria-hidden="true"></i> Buscar</button>

</form>

The URL I would like to have is: midominio.com/coches-en-orihuela-alicante/bmw.html

<?php
if (!empty($_GET['s'])) {
    $s =    $_GET['s'];
}

if (!empty($_GET['categoria'])) {
    $tax_query[] = array(
        'taxonomy' => 'category',
        'field' => 'slug',
        'terms' => $_GET['categoria'],
    );
}

$meta_query['relation'] = 'AND';

if (!empty($_GET['provincia'])) {
    $meta_query[] = array(
        'key'          => 'Provincia',
        'value'          => $_GET['provincia'],
        'compare'     => 'LIKE',
        'type'      => 'string',
    );
}

if (!empty($_GET['poblacion'])) {
    $meta_query[] = array(
        'key'          => 'Ciudad',
        'value'          => str_replace("-", " ", $_GET['poblacion']),
        'compare'     => 'LIKE',
        'type'      => 'string',
    );
}

?>

This give me the URL as: midominio.com/coches-en-orihuela-alicante/bmw.html

but the variables are empty, yes!

I don't know why are empty...

7
  • 1
    What is the URL-friendly structure you would like to have, and what's the code for your search form? And by "custom post field", did you mean this custom field or post meta, or is province a post type slug? Do you have any code which hooks on the pre_get_posts action or another hook? Please edit your post/question and add all that info.
    – Sally CJ
    Commented Jan 27 at 2:05
  • 1
    But in general, you would need to add a custom rewrite rule which rewrites (or internally redirects) a URL like /search/<keyword>/<category>/<province>/<paged>/ to /?s=<keyword>&category=<category>&province=<province>&paged=<paged>. And actually, it should be paged=<page number> and not paged=<page ID>.
    – Sally CJ
    Commented Jan 27 at 2:09
  • edited... I can do wp_redirect, but search results, not show results... page not found
    – Maseres
    Commented Jan 30 at 2:12
  • So, have you now got it working? If not, could you please answer all the questions I asked via my very first comment above? And please, write in English. 🙂
    – Sally CJ
    Commented Feb 1 at 8:01
  • Not work for me... It redirects me, but it doesn't show me the results
    – Maseres
    Commented Feb 3 at 18:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.