尝试以下操作:
PHP
add_action( \'wp_ajax_my_adv_search\', \'ajax_my_adv_search\' );
add_action( \'wp_ajax_nopriv_my_adv_search\', \'ajax_my_adv_search\' );
function ajax_my_adv_search() {
if ( ! check_ajax_referer( \'my-adv-search\', \'q_nonce\', false ) ) {
echo \'session_expired\';
wp_die();
}
$post_type = isset( $_POST[\'q_post_type\'] ) ? $_POST[\'q_post_type\'] : \'\';
$taxonomy = isset( $_POST[\'q_taxonomy\'] ) ? $_POST[\'q_taxonomy\'] : [];
$year = isset( $_POST[\'q_year\'] ) ? $_POST[\'q_year\'] : \'\';
$orderby = isset( $_POST[\'q_orderby\'] ) ? $_POST[\'q_orderby\'] : [];
$order = isset( $_POST[\'q_order\'] ) ? $_POST[\'q_order\'] : \'\';
// Note that if $post_type is \'any\', all post statuses will be included. In
// that case, you may want to set specific post statuses below.
$post_status = \'\';
$taxonomy = array_filter( (array) $taxonomy );
if ( ! in_array( \'any\', $taxonomy ) ) {
$taxonomy = array_unique( array_map( \'trim\', $taxonomy ) );
add_filter( \'posts_join\', function( $c ) use ( $taxonomy ) {
if ( ! empty( $taxonomy ) ) {
global $wpdb;
// 1 below is one/number and not the lowercase of L
$c .= " INNER JOIN {$wpdb->term_relationships} AS ctr1 ON ctr1.object_id = {$wpdb->posts}.ID" .
" INNER JOIN {$wpdb->term_taxonomy} AS ctt1 ON ctt1.term_taxonomy_id = ctr1.term_taxonomy_id";
}
return $c;
} );
add_filter( \'posts_where\', function( $c ) use ( $taxonomy ) {
if ( ! empty( $taxonomy ) ) {
$tax_list = array_map( \'esc_sql\', $taxonomy );
$tax_list = "\'" . implode( "\', \'", $tax_list ) . "\'";
// 1 below is one/number and not the lowercase of L
$c .= " AND ( ctt1.taxonomy IN ($tax_list) )";
}
return $c;
} );
}
if ( ! is_numeric( $year ) ) {
$year = \'\';
}
$orderby = array_filter( (array) $orderby );
if ( in_array( \'any\', $orderby ) ) {
// Don\'t sort by post date.
$orderby2 = false;
} else {
$orderby = array_unique( array_map( \'trim\', $orderby ) );
// TRUE if we\'re sorting by year.
$ob_year = false;
foreach ( $orderby as $i => $s ) {
// Sort posts by year.
if ( \'year\' === $s ) {
$ob_year = true;
unset( $orderby[ $i ] );
}
// Sort posts by views count. Note that this would only return
// posts that have the custom field \'post_views_count\'.
if ( \'views_count\' === $s ) {
$meta_key = \'post_views_count\';
$orderby2 = \'meta_value_num\';
unset( $orderby[ $i ] );
}
}
add_filter( \'posts_orderby\', function( $c, $q ) use ( $ob_year ) {
if ( $ob_year ) {
global $wpdb;
// Use the value parsed by WP_Query.
$order = $q->get( \'order\' );
$c .= $c ? \', \' : \'\';
$c .= "YEAR({$wpdb->posts}.post_date) $order";
}
return $c;
}, 10, 2 );
$ok = isset( $orderby2 );
if ( ! $ok && empty( $orderby ) ) {
// Don\'t sort by post date.
$orderby2 = false;
} elseif ( ! $ok ) {
// Pass to WP_Query as a string.
$orderby2 = implode( \' \', $orderby );
}
}
$q = new WP_Query( [
\'post_status\' => $post_status,
\'post_type\' => $post_type,
\'year\' => $year,
\'meta_key\' => isset( $meta_key ) ? $meta_key : \'\',
\'orderby\' => $orderby2,
\'order\' => $order,
] );
if ( $q->have_posts() ) {
echo \'<ul>\';
while ( $q->have_posts() ) {
$q->the_post();
echo \'<li>\';
the_title(); echo \'; ID: \'; the_ID();
echo \'</li>\';
}
echo \'</ul>\';
} else {
echo \'<p>No posts found.</p>\';
}
wp_die();
}
HTML
查看下面的“表单”,确保在相应的
select
元素/菜单。您还需要在“order by”列中添加“order”字段,并在提交/搜索按钮之前添加nonce字段。
<div id="my-adv-search">
<!-- Post Type -->
<div class="col-md-3">
<div class="form-group">
<label for="q_post_type">Post Type</label>
<select class="form-control" id="q_post_type">
<option value="any" selected>Any</option>
...
</select>
</div>
</div>
<!-- Taxonomy -->
<div class="col-md-3">
<div class="form-group">
<label for="q_taxonomy">Taxonomy</label>
<select class="form-control" multiple id="q_taxonomy">
<option value="any" selected>Any</option>
...
</select>
</div>
</div>
<!-- Year -->
<div class="col-md-3">
<div class="form-group">
<label for="q_year">Year</label>
<select class="form-control" id="q_year">
<option value="any" selected>Any</option>
...
</select>
</div>
</div>
<!-- Orderby -->
<div class="col-md-3">
<div class="form-group">
<label for="q_orderby">Order by</label>
<select class="form-control" multiple id="q_orderby">
<option value="any" selected>Any</option>
<?php
foreach ( [
\'author\' => \'Author\',
\'comment_count\' => \'Popularity (# of Comments)\',
\'year\' => \'Year\',
\'views_count\' => \'Views\',
] as $value => $label ) {
printf( \'<option value="%s">%s</option>\',
esc_attr( $value ), esc_html( $label ) );
}
?>
</select>
</div>
<label class="radio-inline">
<input type="radio" name="order" id="q_order-asc" value="ASC" />
ASC
</label>
<label class="radio-inline">
<input type="radio" name="order" id="q_order-desc" value="DESC" checked />
DESC
</label>
</div>
<!-- Nonce field. -->
<?php wp_nonce_field( \'my-adv-search\', \'q_nonce\' ); ?>
<!-- Search Button -->
<div class="col-md-12">
<input type="submit" class="btn btn-primary" id="buscar_btn" value="Search">
<noscript><b>Your browser does not support Javascript, this making it unable to display the posts.</b></noscript>
<div id="resultados"><div class="cargando_medio"></div></div>
</div>
</div><!-- End #my-adv-search -->
JS/jQuery/AJAX
下面是一个执行AJAX搜索的JS/jQuery脚本示例。
jQuery( function( $ ){
var ajaxurl = \'/path/to/wp-admin/admin-ajax.php\';
function searchPosts( btn ) {
var _btn_text = btn.value,
q_order;
btn.disabled = true;
btn.value = \'Searching..\';
q_order = $( \'#q_order-asc\' ).is( \':checked\' ) ?
\'ASC\' : \'DESC\';
return $.post( ajaxurl, {
action: \'my_adv_search\',
q_nonce: $( \'#q_nonce\' ).val(),
q_post_type: $( \'#q_post_type\' ).val(),
q_taxonomy: $( \'#q_taxonomy\' ).val(),
q_year: $( \'#q_year\' ).val(),
q_orderby: $( \'#q_orderby\' ).val(),
q_order: q_order,
} ).done( function( s ){
if ( \'session_expired\' === s ) {
location.reload();
return;
}
$( \'#resultados\' ).html( s );
} ).always( function(){
btn.value = _btn_text;
btn.disabled = false;
} );
}
$( \'#buscar_btn\', \'#my-adv-search\' ).on( \'click\', function( e ){
e.preventDefault();
// Run AJAX search.
searchPosts( this );
// Remove button focus.
this.blur();
} );
} );
Notes
<在
ajax_my_adv_search()
PHP函数,我没有使用
tax_query
使用
WP_Query
调用,因为我们正在查询位于其中一个指定分类键中的帖子(例如。
category
对于标准职位类别,以及
post_tags
对于标准Post标记),而不是在分类法内的术语中。因此,我使用
posts_join
和
posts_where
钩住
WP_Query
获取此类查询的正确结果。
中的ajax_my_adv_search()
PHP函数,我还使用了posts_orderby
钩住WP_Query
因为默认情况下,按年度排序帖子/结果在WP_Query
班