我已创建自定义帖子类型exam_result
具有academic_year
作为自定义分类法,school_class
使用高级自定义字段插件作为自定义分类法,并将卷号作为自定义字段。
问题是,我只想在帖子具有自定义分类法和最重要的卷号的正确值时显示结果。现在,即使我没有选择任何分类法或输入任何卷号,它也会返回帖子。
我曾尝试使用分类关系,但未能使其发挥作用。我已经在下面尽可能多地介绍了细节。我想知道这里是否有人能帮我找出我的代码可能有什么问题。
Search Form
<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="filter">
<fieldset>
<?php
if( $terms = get_terms( \'academic_year\' ) ) :
echo \'<select value ="" name="academic_year"><option>Select Academic Year</option>\';
foreach ( $terms as $term ) :
echo \'<option value="\' . $term->term_id . \'">\' . $term->name . \'</option>\';
endforeach;
echo \'</select>\';
endif;?>
<?php
if( $terms = get_terms( \'school_class\') ) :
echo \'<select value ="" name="school_class"><option>Select Class</option>\';
foreach ( $terms as $term ) :
echo \'<option value="\' . $term->term_id . \'">\' . $term->name . \'</option>\';
endforeach;
echo \'</select>\';
endif;?>
<input type="text" name="roll_number" placeholder="Enter Roll Number" id="roll-number" />
<button class="exam-result-container">Submit</button>
<input type="hidden" name="action" value="myfilter">
</fieldset>
</form>
Query that I have placed in the functions.php file
function exam_result_filter_function(){
$args = array(
\'post_type\' => \'exam_result\',
);
if( isset( $_POST[\'school_class\'] ) || !empty( $_POST[\'academic_year\']) || !empty( $_POST[\'roll_number\']) )
$args[\'tax_query\'] = array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'school_class\',
\'field\' => \'id\',
\'terms\' => $_POST[\'school_class\'],
),
array(
\'taxonomy\' => \'academic_year\',
\'field\' => \'id\',
\'terms\' => $_POST[\'academic_year\'],
),
array(
\'key\' => \'roll_number\',
\'value\' => $_POST[\'roll_number\'],
),
);
$query = new WP_Query( $args );
print_r($args);
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post();
echo the_title();
endwhile;
wp_reset_postdata();
else :
get_template_part( \'no-results\', \'page\' );
endif;
die();
}
add_action(\'wp_ajax_myfilter\', \'exam_result_filter_function\');
add_action(\'wp_ajax_nopriv_myfilter\', \'exam_result_filter_function\');
HTML Where the result will be inserted:
<div id="exam-result-container">
</div>
JavaScript:
<script>
jQuery(document).ready(function($){
$(\'#filter\').submit(function(){
var filter = $(\'#filter\');
$.ajax({
url:filter.attr(\'action\'),
data:filter.serialize(),
type:filter.attr(\'method\'),
beforeSend:function(xhr){
filter.find(\'button\').text(\'Processing..\');
},
success:function(data) {
filter.find(\'button\').text(\'Submit\');
$(\'#exam-result-container\').html(data);
}
});
return false;
});
});
</script>
最合适的回答,由SO网友:Bhupen 整理而成
您已经在tax\\u查询中设置了“roll\\u number”自定义字段。它应该在“meta\\u query”中。请尝试更新的代码:
tion exam_result_filter_function(){
$args = array(
\'post_type\' => \'exam_result\',
);
if( isset( $_POST[\'school_class\'] ) || !empty( $_POST[\'academic_year\']) || !empty( $_POST[\'roll_number\']) ){
$args[\'tax_query\'] = array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'school_class\',
\'field\' => \'id\',
\'terms\' => $_POST[\'school_class\'],
),
array(
\'taxonomy\' => \'academic_year\',
\'field\' => \'id\',
\'terms\' => $_POST[\'academic_year\'],
)
);
$args[\'meta_query\'] = array(
array(
\'key\' => \'roll_number\',
\'value\' => $_POST[\'roll_number\'],
\'compare\' => \'IN\',
),
);
}
$query = new WP_Query( $args );
print_r($args);
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post();
echo the_title();
endwhile;
wp_reset_postdata();
else :
get_template_part( \'no-results\', \'page\' );
endif;
die();
}
add_action(\'wp_ajax_myfilter\', \'exam_result_filter_function\');
add_action(\'wp_ajax_nopriv_myfilter\', \'exam_result_filter_function\');