View difference between Paste ID: ctGpKUJB and kEaPUtVH
SHOW: | | - or go back to the newest paste.
1
<?php
2
3
function video_filter_function() {
4
    check_ajax_referer( 'my_nonce' );
5
6
    $date = sanitize_text_field( $_POST['date'] );
7
    $date = ( $date === 'ASC' ) ? 'ASC' : 'DESC'; // Make sure it's a valida date
8
9
    $args = array(
10
        'post_type' => 'videos',
11
        'orderby'   => 'date',
12
        'order'     => $date,
13
    );
14
15
    // Add to query only if it's not empty
16
    if ( ! empty( $_POST['categoryfilter'] ) ) {
17
        $categoryfilter = sanitize_text_field( $_POST['categoryfilter'] );
18
19
        $args['tax_query'] = array(
20
            array(
21
                'taxonomy'  => 'category',
22
                'field'     => 'id',
23
                'terms'     => $categoryfilter,
24
            ),
25
        );
26
    }
27
28
    // Add to query only if it's not empty
29
    if ( ! empty( $_POST['description'] ) ) {
30
        $description = sanitize_text_field( $_POST['description'] );
31
32
        $args['meta_query'] = array(
33
            array(
34
                'key'       => 'video_description',
35
                'value'     => $description,
36
                'compare'   => 'LIKE',
37
            ),
38
        );
39
    }
40
41
    $query = new WP_Query( $args );
42
43
    if ( $query->have_posts() ) :
44
        while( $query->have_posts() ) : $query->the_post();
45
    ?>
46
47
        <div class="video-grid-item">
48
            <h2> <?php the_field('video_title'); ?></h2>
49
            <p> <?php the_field('video_description'); ?></p>
50
            <iframe src="https://player.vimeo.com/video/<?php the_field( 'vimeo' ); ?>" width="640" height="640" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
51
        </div>
52
53
<?php
54
        endwhile;
55
        wp_reset_postdata();
56
    else :
57
        echo 'No posts found';
58
    endif;
59
60
    die; // Search for wp_send_json() to check how to use the JSON away
61
}
62
63
64
add_action( 'wp_ajax_myfilter', 'video_filter_function' );
65
add_action( 'wp_ajax_nopriv_myfilter', 'video_filter_function' );