custom post types – Internal Server Error when echoing an OBJECT from wp ajax action function in functions.php to JS file

Question

I’m using Elementor Pro as my page builder. I’m trying to create a Job Post filter using Elementor forms. Issue is when I echo and return an Object type from the functions.php file to the JS file. It gives out an Internal Server Error 500. I want to know how I can pass the whole post object instead of sending Title, excerpt, image separately.

JS code

jQuery(function($){
    $('#jobs_filter_form').submit(function(){
        filter = $('#jobs_filter_form');
        let keywords = $('#form-field-job_keywords').val();
        let location = $('#form-field-job_location').val();
        let category = $('#form-field-job_category').val();
        $.ajax({
            url:'http://localhost/placementpro/wp-admin/admin-ajax.php',
            data:{
                'action': 'filter_jobs',
                'keywords': keywords,
                'location': location,
                'category': category,
            },
            type:'POST',
            beforeSend:function(xhr){
                filter.find('button').text('Processing...'); // changing the button label
            },
            success:function(data){
                filter.find('button').text('Apply filter'); // changing the button label back
                $('#filtered_jobs').html(data);
            },
        });
        return false;
    });
});

PHP code

add_action('wp_ajax_filter_jobs', 'filter_jobs_function');
add_action('wp_ajax_nopriv_filter_jobs', 'filter_jobs_function');

function filter_jobs_function(){
    $keywords = $_POST['keywords'];    
    $location = $_POST['location'];    
    $category = $_POST['category'];
        
    $args = array(
        'post_type'         => 'kuwjr_job',
        'cache_results'     => false,
        'update_post_meta_cache' => false,
        'update_post_term_cache' => false,
        'fields'            => 'all',
        'post_status'       => 'publish',
        'posts_per_page'    => 12,
        'orderby'           => 'date',
        'order'             => 'DESC',
        's'                 => $keywords,
        'meta_query'        => array(
            array(
                'key'   => 'location',
                'value' => $location,
            ),
        ),
        'tax_query'         => array(
            array(
                'taxonomy'  => 'job-category',
                'terms'     => $category,
                'field'     => 'name',
            ),
        ),
    );

    $query = new WP_Query( $args );

    if( $query->have_posts() ):
        while( $query->have_posts() ): $query->the_post();
            echo $query->post;
        endwhile;
        wp_reset_postdata();
    else :
        echo 'No posts found';
    endif;

    die();
}

0
shimii 1 month 2022-12-16T07:14:57-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse