php Filtros de listas para o WordPress(palavra-chave + taxonomia + post meta)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Filtros de listas para o WordPress(palavra-chave + taxonomia + post meta)相关的知识,希望对你有一定的参考价值。
<form class="row w-100 form-cursos" method="get">
<input type="hidden" name="ptype" id="ptype" value="curso">
<input type="hidden" name="ppp" id="ppp" value="9">
<input type="hidden" name="paged" id="paged" value="1">
<input type="hidden" name="order" id="order" value="ASC">
<input type="hidden" name="orderby" id="orderby" value="post_date">
<div class="col-md-4">
<div class="dropdown w-100">
<button class="btn btn-outline-secondary dropdown-toggle w-100" type="button" id="area-de-interesse" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<?php
if ( isset( $_GET['taxquery']['term'][0] ) && !empty( $_GET['taxquery']['term'][0] ) ) {
$tx0 = strip_tags( sanitize_text_field( $_GET['taxquery']['term'][0] ) );
} else {
echo 'Área de interesse';
}
?>
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<a class="dropdown-item ddown-filtro" href="javascript:;" data-val="">Área de interesse</a>
<?php
$terms = get_terms( 'area_de_interesse', array(
'hide_empty' => false,
) );
for ( $i=0; $i<count( $terms ); $i++ ) {
?>
<a class="dropdown-item ddown-filtro" href="javascript:;" data-val="<?php
echo $terms[$i]->slug;
?>"><?php
echo $terms[$i]->name;
?></a>
<?php
}
?>
</div>
<input type="hidden" id="taxquery[term][0]" name="taxquery[term][0]" value="<?php echo strip_tags( sanitize_text_field( $_GET['taxquery']['term'][0] ) ); ?>">
<input type="hidden" id="taxquery[taxonomy][0]" name="taxquery[taxonomy][0]" value="area_de_interesse">
</div>
</div>
<div class="col-md-3">
<div class="dropdown w-100">
<button class="btn btn-outline-secondary dropdown-toggle w-100" type="button" id="tipo_de_curso" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<?php
if ( isset( $_GET['metaquery']['value'][0] ) && !empty( $_GET['metaquery']['value'][0] ) ) {
$tx0 = strip_tags( sanitize_text_field( $_GET['metaquery']['value'][0] ) );
} else {
echo 'Tipo de curso';
}
?>
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<a class="dropdown-item ddown-filtro" href="javascript:;" data-val="">Tipo de curso</a>
<a class="dropdown-item ddown-filtro" href="javascript:;" data-val="bacharelado">Bacharelado</a>
<a class="dropdown-item ddown-filtro" href="javascript:;" data-val="licenciatura">Licenciatura</a>
<a class="dropdown-item ddown-filtro" href="javascript:;" data-val="tecnologo">Tecnólogo</a>
</div>
<input type="hidden" id="metaquery[value][0]" name="metaquery[value][0]" value="<?php echo strip_tags( sanitize_text_field( $_GET['metaquery']['value'][0] ) ); ?>">
<input type="hidden" id="metaquery[key][0]" name="metaquery[key][0]" value="tipo_de_curso">
</div>
</div>
<div class="col-md-5">
<input type="submit" name="enviar" id="enviar-btn"><input type="text" placeholder="Encontre seu curso!" name="s" id="termo-filtro"><a href="javascript:;" id="limpar-busca"></a>
</div>
</form><!-- fim row w-100 -->
<div class="row w-130" id="retorno-lista">
</div>
<div class="row w-100" id="paginacao">
</div>
////////////////////////////////////////////////////////
// filtros de listas de conteudo
var campobuscakup;
$('#limpar-busca').click(function(){
if ( $(this).prev().val() != '' ) {
$(this).prev().val('');
$('#paged').val( 1 );
fc_filtrar();
}
});
$('.ddown-filtro').click(function(){
$(this).parent().next().val( $(this).attr('data-val') );
$('#paged').val( 1 );
fc_filtrar();
});
$('#termo-filtro').keyup(function(){
$('#paged').val( 1 );
clearTimeout( campobuscakup );
campobuscakup = setTimeout( function(){ fc_filtrar(); }, 500 );
});
$(document).on('click', '.ajaxando a', function(e){
e.preventDefault();
var num = $(this).attr('href').split('/page/');
num = num[1].replace('/','');
$('#paged').val( num );
fc_filtrar();
});
function fc_filtrar () {
data = { action: 'filtro' };
var d = 'action=filtro';
if ( $('#ptype').length > 0 ) {
d = d + '&ptype=' + $('#ptype').val();
}
if ( $('#ppp').length > 0 ) {
d = d + '&ppp=' + $('#ppp').val();
}
if ( $('#orderby').length > 0 ) {
d = d + '&orderby=' + $('#orderby').val();
}
if ( $('#order').length > 0 ) {
d = d + '&order=' + $('#order').val();
}
if ( $('#paged').length > 0 ) {
d = d + '&paged=' + $('#paged').val();
}
var alltax = $("input[ name^=taxquery\\[term\\] ]");
for (i = 0; i < alltax.length; i++) {
d = d + '&taxquery[term][' + i + ']=' + alltax.eq(i).val();
}
var alltax = $("input[ name^=taxquery\\[taxonomy\\] ]");
for (i = 0; i < alltax.length; i++) {
d = d + '&taxquery[taxonomy][' + i + ']=' + alltax.eq(i).val();
}
var alltax = $("input[ name^=metaquery\\[value\\] ]");
for (i = 0; i < alltax.length; i++) {
d = d + '&metaquery[value][' + i + ']=' + alltax.eq(i).val();
}
var alltax = $("input[ name^=metaquery\\[key\\] ]");
for (i = 0; i < alltax.length; i++) {
d = d + '&metaquery[key][' + i + ']=' + alltax.eq(i).val();
}
if ( $('#termo-filtro').length > 0 ) {
d = d + '&s=' + $('#termo-filtro').val();
}
$('#retorno-lista').html('');
$('#paginacao').html('');
$.ajax({
type : 'POST',
url : ajaxurl,
data : d,
processData: false,
success:function(data) {
data = JSON.parse( data );
$('#retorno-lista').html( data.lista );
$('#paginacao').html( data.paginacao );
$('#paginacao').addClass('ajaxando');
},
error: function(errorThrown){
console.log( errorThrown );
}
});
}
///////////////////////////////////////////////////////
// filtros
function fc_filtro () {
global $_GET, $_POST, $wp_query;
if ( count($_POST) > 0 ) {
$vars = $_POST;
} else {
$vars = $_GET;
}
if ( strlen( $vars['ptype'] ) == 0 ) {
$ptype = 'post';
} else {
$ptype = $vars['ptype'];
}
if ( strlen( $vars['ppp'] ) == 0 ) {
$ppp = 9;
} else {
$ppp = (int)$vars['ppp'];
}
if ( strlen( $vars['orderby'] ) == 0 ) {
$orderby = 'post_title';
} else {
$orderby = $vars['orderby'];
}
if ( strlen( $vars['order'] ) == 0 ) {
$order = 'ASC';
} else {
$order = $vars['order'];
}
if ( strlen( $vars['paged'] ) == 0 ) {
$paged = 1;
} else {
$paged = (int)$vars['paged'];
}
$args = array(
'post_type' => $ptype,
'posts_per_page' => $ppp,
'orderby' => $orderby,
'order' => $order,
'paged' => $paged,
);
if ( count( $vars['taxquery']['taxonomy'] ) > 0 ) {
for ( $i=0; $i < count( $vars['taxquery']['taxonomy'] ); $i++ ) {
if ( strlen( $vars['taxquery']['term'][$i] ) > 0 ) {
$args['tax_query'][] = array(
'taxonomy' => $vars['taxquery']['taxonomy'][$i],
'field' => 'slug',
'terms' => $vars['taxquery']['term'][$i],
);
}
}
}
if ( count( $vars['metaquery']['value'] ) > 0 ) {
for ( $i=0; $i < count( $vars['metaquery']['value'] ); $i++ ) {
if ( strlen( $vars['metaquery']['value'][$i] ) > 0 ) {
$args['meta_query'][] = array(
'key' => $vars['metaquery']['key'][$i],
'value' => $vars['metaquery']['value'][$i],
);
}
}
}
$args['s'] = $vars['s'];
wp_reset_query();
$wp_query = new WP_Query( $args );
if( $wp_query->have_posts() ) {
ob_start();
while ($wp_query->have_posts()) : $wp_query->the_post();
include( locate_template( '_inc/um-curso.php', false, false ) );
endwhile;
$lista = ob_get_contents();
ob_end_clean();
$pag = wp_pagination( $paged, $ppp );
wp_reset_query();
} else {
$lista = '<div class="alert alert-warning" style="margin-left: 15px; calc( 100% - 15px);">Nenhum resultado para os filtros selecionados. Tente outras opções.</div>';
}
$ret = array(
'lista' => $lista,
'paginacao' => $pag,
);
$ret = json_encode($ret);
echo $ret;
wp_die();
}
add_action( 'wp_ajax_filtro', 'fc_filtro' );
add_action( 'wp_ajax_nopriv_filtro', 'fc_filtro' );
以上是关于php Filtros de listas para o WordPress(palavra-chave + taxonomia + post meta)的主要内容,如果未能解决你的问题,请参考以下文章
text Eliminar los filtros por defecto de magento y agregar nuevos filtros de atributos custom
sql sp2RPT_CES_ObtenerReporteSIE - Se agreganlainformaciónfaltantey los filtros necesario par
PHP Crear una lista de carpetas con PHP
php Ocultar插件da lista de Plugins
javascript DataTables:obtener valores de columnas para crear filtros