最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Display a post from custom post type only if all the selected taxonomies and custom field value matches the record

programmeradmin1浏览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>

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>
Share Improve this question edited Jul 1, 2019 at 8:38 Kevin S asked Jul 1, 2019 at 7:54 Kevin SKevin S 371 silver badge8 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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');

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论