1

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.

0

1 Answer 1

2

Try 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');
8
  • Thank you very much good Sir, I tried to upvote your answer but I couldn't because i haven't fullfilled the reputation requirement. I am not too sure, if I can ask you this question here, Please forgive me if i am not suppose to ask it here. SO here is the question,the ajax is working fine as of now, however wp_query is not working as in the functions.php. I am not too sure if I am calling it correctly. Are you able to advise on this? thank you
    – desh
    Commented Jan 26, 2019 at 5:19
  • Yes, you can use WP_Query in ajax, try removing spaces $query->have_posts(), this may be issue. also if not working provide the error what you are getting there. Commented Jan 26, 2019 at 6:41
  • thank you and sorry for the late reply . I was away due to a knee injury. So the current update of this issue is that i am able to load it via ajax, thanks to you. However, when functions.php loads the the_content() , it spits out html code like this prntscr.com/mjy2vj , i suspect this is probably due to posts was created by a plugin call divi. are you able to advise on this? thank you
    – desh
    Commented Feb 12, 2019 at 9:04
  • just change the_content like: echo apply_filters( 'the_content', get_the_content() ); Commented Feb 12, 2019 at 10:25
  • actually i did do that in my functions.php file I have inserted this $content = apply_filters( 'the_content' , wp_strip_all_tags(get_the_content())); but still it spits out the html.
    – desh
    Commented Feb 12, 2019 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.