0

I have created a custom post type exam_result with academic_year as a custom taxonomy, school_class as a custom taxonomy and roll number as a custom filed using the Advance Custom Field plugin.

The issue is that I want to display the result only if the post has the correct value for both the custom taxonomy and most importantly roll number. Right now it returns the posts even if I do not select any taxonomy or enter any roll number.

I have tried using Taxonomy Relationships but could not get it to work. I have included as many details as possible below. I am wondering if someone here can help me figure out what might be wrong with my code.

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>
1

You have set "roll_number" custom field in tax_query. It should be in "meta_query". Please try the updated code:

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');
  • 1
    Try to print your query to see the result. print_r($query); – Bhupen Jul 1 at 8:58
  • 1
    @kevin change 'field' => 'id' to 'term_id' and let me know if you are still facing the issue. – Bhupen Jul 1 at 10:10
  • 1
    No, because I have tried with the group also and it is working fine. – Bhupen Jul 1 at 12:00
  • 1
    Are you sure, @Bhupen? Because for sub-fields in a Group field, the meta key is {group name/slug}_{sub-field name/slug}.. :) So @KevinS, try with 'key' => 'students_information_roll_number' in the meta query. – Sally CJ Jul 1 at 14:43
  • 1
    Kevin can you please share your ACF field exported JSON file so that I can configure the same on my end and check. @SallyCJ After importing ACF fields I will be able to check properly on my end. – Bhupen Jul 2 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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