Home › Forums › General Issues › Relationship of city and state fields
How do I create a filter where I select the state and load another combo with the states … how do I do this relationship can guiding me?
Here is my adaptation:
<div class="container centered">
<div class="row">
<div class="col-xs-12 col-md-4 col-md-offset-4">
<h1>Estado</h1>
<div class="row">
<div class="col-xs-12 col-sm-8 col-md-8">
<div id="criaComboCidade">
<?php criaComboEstado(); ?>
</div>
<h1>Cidade</h1>
<div id="criaComboEstado">
<?php criaComboCidade('Paraná'); ?>
</div>
</div>
<div class="col-xs-12 col-sm-4 col-md-4">
<div class="float-right">
<button class="btn btn-primary" onclick="asyncEstado(document.getElementById('cmbEstado').value)">Pesquisar</button>
</div>
</div>
</div>
</div>
</div>
</div>
<br>
<br>
<br>
<div class="container">
<div class="row" id="informacoes">
<?php
$estado = $_POST['cmbEstado'];
$cidade = $_POST['cmbCidade'];
if ($cidade == null) {
$args = array(
'post_type' => 'assistencia_tecnica',
'order' => 'ASC',
'meta_key' => estado,
'meta_value' => $estado
);
}
else {
$args = array(
'post_type' => 'assistencia_tecnica',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'estado',
'value' => $estado,
),
array(
'key' => 'cidade',
'value' => $cidade,
),
),
);
};
$consulta = new wp_query($args);
?>
<?php while($consulta->have_posts()) : $consulta->the_post(); ?>
<div class="col-xs-12 col-xs-6">
<br><i class="fa fa-caret-right" aria-hidden="true"> </i><b> <?php the_title();?></b><br>
<label>Telefone(s):</label> <br>
<?php if(get_field('telefone1') != null): ?>
<i class="fa fa-mobile" aria-hidden="true"> </i> <?php the_field('telefone1') ?><br>
<?php endif; ?>
<?php if(get_field('telefone2') != null): ?>
<i class="fa fa-mobile" aria-hidden="true"> </i> <?php the_field('telefone2') ?><br>
<?php endif; ?>
<?php if(get_field('telefone3') != null): ?>
<i class="fa fa-mobile" aria-hidden="true"> </i> <?php the_field('telefone3') ?>
<?php endif; ?>
<br><i class="fa fa-map-marker" aria-hidden="true"> </i> <strong> Endereço:</strong> <?php echo the_field('endereco') ?> <strong><?php echo the_field('cidade') ?> - <?php echo the_field('estado') ?></strong>
<br><i class="fa fa-envelope-o" aria-hidden="true"> </i> <?php echo the_field('email') ?>
</div>
<?php endwhile; ?>
</div>
<br>
<br>
<br>
<br>
</main>
<script>
function asyncEstado(param) {
$.ajax({
type:"POST",
data:{ cmbEstado : param },
success: function(data){
parser = new DOMParser();
datahtml = parser.parseFromString(data, "text/html");
$("#informacoes").html(datahtml.getElementById("informacoes").innerHTML)
},
error: function(data){
console.log("error")
}
})
}
</script>
<?php get_footer(); ?>
<?php
function criaComboCidade($buscacidade){
$added = array();
$args = array(
'post_type' => 'assistencia_tecnica',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'estado',
'value' => $buscacidade,
'compare' => '='
),
array(
'key' => 'cidade',
),
),
);
$query = new WP_Query( $args );
if($query->have_posts()) :
?>
<select name="cmbCidade" class="form-control" aria-required="true" aria-invalid="false" id="cmbCidade" onchange="asyncEstado(document.getElementById('cmbCidade').value)">
<option>--</option>
<?php
while($query->have_posts()) :
$query->the_post();
// get cidade
$lpcidade = get_field('cidade');
// DISTINCT
if( in_array($lpcidade, $added) )
{
continue;
}
// add estado
$added[] = $clpidade
//loop
?>
<option value="<?php echo $lpcidade ?>"><?php echo $lpcidade ?></option>
<?php endwhile;
echo "</select>";
endif;
};
?>
<?php
function criaComboEstado(){
$added = array();
$args = array(
'post_type' => 'assistencia_tecnica',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'estado',
)
),
);
$query = new WP_Query( $args );
if($query->have_posts()) :
?>
<select name="cmbEstado" class="form-control" aria-required="true" aria-invalid="false" id="cmbEstado" onchange="change(document.getElementById('cmbEstado').value);" >
<?php
while($query->have_posts()) :
$query->the_post();
// "Pega" o campo
$lpestado = get_field('estado');
// DISTINCT
if( in_array($lpestado, $added) )
{
continue;
}
// add estado
$added[] = $lpestado
//inicia o loop
?>
<option value="<?php echo $lpestado ?>"><?php echo $lpestado ?></option>
<?php endwhile;
echo "</select>";
endif;
}
?>
The topic ‘Relationship of city and state fields’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.