admin管理员组

文章数量:1125957

I have this url:

mydomain/?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/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/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/coches-en-orihuela-alicante/bmw.html

but the variables are empty, yes!

I don't know why are empty...

本文标签: customizationRewrite URL custom search query