admin管理员组文章数量:1202337
So I have a HTML select form which allows users to choose years such as 2017 , 2018 etc
Hence, what i want this to achieve is when user selects 2017 , the posts from 2017 will be loaded on the page.
My process thought was to use AJAX along with WP_QUERY to accomplish this.
However, the error i am receiving is ERROR 400 BAD REQUEST.
Thus any help given will be greatly appreciated.
Front end
<form action="<?php echo site_url()?>/wp-admin/admin-ajax.php" method="POST" id="filter>
<select id="year"></select>
</form>
JS ajax request
$.ajax({
url : filter.attr('action') ,
data : filter.serialize() ,
type : filter.attr('method') ,
success : function (response)
{
console.log(response)
} ,
error : function (request , status , error)
{
console.log(request.responseText)
console.log(error)
} ,
headers: {
'Content-type': 'application/x-www-form-urlencoded'
} ,
datatype : 'xml'
}).done(function () {
console.log('ajax completed')
})
functions.php
function filter_post_asb
{
$args = array(
'cat' => 43 ,
'year' => $_POST['year']
);
$query = new WP_Query($args);
if ( $query -> have_posts()) :
?>
<div class="container">
<select id="year"></select>
<ul class="slides">
<?php while($query -> have_posts()) : $query -> the_post(); ?>
<li>
<img src="<?php bloginfo('template_url')?>/images/logo.png" >
<?php the_content(); ?>
<h2><?php the_permalink();?></h2>
<h4><?php the_title();?></h4>
</li>
<li>
<?php the_post_thumbnail();?>
</li>
<?php endwhile ; ?>
<?php wp_reset_postdata();?>
</ul>
</div>
<?php
endif;
}
add_action('wp_ajax_filter', 'filter_post_asb');
add_action('wp_ajax_nopriv_filter', 'filter_post_asb');
Once again, thank you very much for all the input given.
So I have a HTML select form which allows users to choose years such as 2017 , 2018 etc
Hence, what i want this to achieve is when user selects 2017 , the posts from 2017 will be loaded on the page.
My process thought was to use AJAX along with WP_QUERY to accomplish this.
However, the error i am receiving is ERROR 400 BAD REQUEST.
Thus any help given will be greatly appreciated.
Front end
<form action="<?php echo site_url()?>/wp-admin/admin-ajax.php" method="POST" id="filter>
<select id="year"></select>
</form>
JS ajax request
$.ajax({
url : filter.attr('action') ,
data : filter.serialize() ,
type : filter.attr('method') ,
success : function (response)
{
console.log(response)
} ,
error : function (request , status , error)
{
console.log(request.responseText)
console.log(error)
} ,
headers: {
'Content-type': 'application/x-www-form-urlencoded'
} ,
datatype : 'xml'
}).done(function () {
console.log('ajax completed')
})
functions.php
function filter_post_asb
{
$args = array(
'cat' => 43 ,
'year' => $_POST['year']
);
$query = new WP_Query($args);
if ( $query -> have_posts()) :
?>
<div class="container">
<select id="year"></select>
<ul class="slides">
<?php while($query -> have_posts()) : $query -> the_post(); ?>
<li>
<img src="<?php bloginfo('template_url')?>/images/logo.png" >
<?php the_content(); ?>
<h2><?php the_permalink();?></h2>
<h4><?php the_title();?></h4>
</li>
<li>
<?php the_post_thumbnail();?>
</li>
<?php endwhile ; ?>
<?php wp_reset_postdata();?>
</ul>
</div>
<?php
endif;
}
add_action('wp_ajax_filter', 'filter_post_asb');
add_action('wp_ajax_nopriv_filter', 'filter_post_asb');
Once again, thank you very much for all the input given.
Share Improve this question edited Jan 25, 2019 at 12:07 fuxia♦ 107k38 gold badges255 silver badges459 bronze badges asked Jan 25, 2019 at 10:42 deshdesh 1372 silver badges10 bronze badges 01 Answer
Reset to default 2Try by change appropriate code:
Front end
<form action="<?php echo site_url()?>/wp-admin/admin-ajax.php" method="POST" id="filter>
<select name="year"></select>
<input type="hidden" name="action" value="filter" />
</form>
JS ajax request
$.ajax({
url : filter.attr('action') ,
data : filter.serialize() ,
type : filter.attr('method') ,
success : function (response)
{
console.log(response)
},
error : function (request , status , error)
{
console.log(request.responseText)
console.log(error)
}
}).done(function () {
console.log('ajax completed')
})
functions.php
function filter_post_asb {
ob_start()
$args = array(
'cat' => 43,
'year' => $_POST['year']
);
$query = new WP_Query($args);
if ( $query -> have_posts()) : ?>
<div class="container">
<select id="year"></select>
<ul class="slides">
<?php while($query -> have_posts()) : $query -> the_post(); ?>
<li>
<img src="<?php bloginfo('template_url')?>/images/logo.png" >
<?php the_content(); ?>
<h2><?php the_permalink();?></h2>
<h4><?php the_title();?></h4>
</li>
<li>
<?php the_post_thumbnail();?>
</li>
<?php endwhile ; ?>
<?php wp_reset_postdata();?>
</ul>
</div>
<?php
endif;
echo ob_get_clean();
// OR
// $html = ob_get_clean();
// echo json_encode( array( 'html' => $html ) );
exit;
}
add_action('wp_ajax_filter', 'filter_post_asb');
add_action('wp_ajax_nopriv_filter', 'filter_post_asb');
本文标签: wp queryAJAX filter posts by year
版权声明:本文标题:wp query - AJAX filter posts by year 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738617959a2103019.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论