How to loop year & month condition together to filter correctly via AJAX?

Question

So I’ve been trying to create a filter function via AJAX that can help view the latest postings of a custom type post.

So far I’m able to work out the AJAX part, and even the Year filter, however i am stuck where I need to give a scenario where when the page first loads, to load all months of the year if there’s no specific month, and when there IS a specific month, to only load those postings in that month of that same year.

Let me show you the codes:

        //Search Media According to Year Select
        if(isset($_GET['year_select'])) {
            $temp = sanitize_text_field(($_GET['year_select']));
            $year_select = intval($temp);
        } else {
            $year_select = $latest_year;
        }

        $date_query_array = array(
            'year' => $year_select
        );

        $paged = get_query_var( 'paged' );

        $year_args = array(
            'post_type' => 'media',
            'posts_per_page' => 8,
            'meta_query' => $query_array,
            'date_query' => $date_query_array,
            'orderby' => 'date',
            'order' => 'DESC',
            'paged' => $paged
        );

        $specific_year = new WP_Query($year_args);
        //End Searching

And here’s the loop:

                    if($specific_year->have_posts()) {
                        while($specific_year->have_posts()) {
                            $specific_year->the_post();

                            $title = get_the_title();
                            $media_news_location = get_field('media_news_location');

                ?>
                    <tr class="news-events-tablerow">
                        <td class="news-events"><h4><?php echo get_the_date('d/m/Y'); ?></h4></td>
                        <td class="news-events">
                            <a href="<?php the_permalink() ?>">
                            <h3><?php echo $title; ?></h3></a>
                            <b><?php echo $media_news_location?></b> - <?php echo wp_trim_words( get_the_content(), 30, '... <a href="' . get_permalink() . '">Read more</a>')?>

From what I understand, I need a way to modify the loop such that
– on the first visit & the variable month is not set, to show all postings of that month of the latest year
– after setting the filter for a specific month, to only show postings of that month only, of that year.

EDIT

<?php
                    if($specific_year->have_posts()) {
                        while($specific_year->have_posts()) {
                            $specific_year->the_post();

                            $title = get_the_title();
                            $media_news_location = get_field('media_news_location');


                            require get_template_directory().'/inc/process_news_title.php';
                ?>
                    <tr class="news-events-tablerow">
                        <td class="news-events"><h4><?php echo get_the_date('d/m/Y'); ?></h4></td>
                        <td class="news-events">
                            <?php 
                            if ( $item['archived'] ) { 
                                $pdf = get_field('media_'.$post_slug.'pdf');
                                $pdf = $pdf['url'];
                            } else {

                                if($media_type == "event"):
                                    $event = get_field('media_event_pdf');
                                    $pdf = $event['url'];
                                else:
                                    $pdf = get_field('media_archived_link');
                                endif;


                            }
                            ?>
                                <!-- <a href="<?php echo $pdf ?>" target="_blank"> -->
                                    <a href="<?php the_permalink() ?>">
                                        <h3><?php echo $title; ?></h3></a>
                                    <b><?php echo $media_news_location?></b> - <?php echo wp_trim_words( get_the_content(), 30, '... <a href="' . get_permalink() . '">Read more</a>')?>
                                        <img src=""><?php echo get_the_post_thumbnail_url(); ?>
                                </a>
                        </td>
                    </tr>
                <?php } wp_reset_postdata(); ?>
                </table>
                <div class="pub-pagination">
                    <?php echo paginate_links(array(
                        'total' => $specific_year->max_num_pages
                    )); ?>
                </div>
                <?php } ?>
            </div>
        </div>
    </div>

Something’s wrong!

0
Christopher Tock 2 years 2020-01-02T08:38:35-05:00 0 Answers 79 views 0

Leave an answer

Browse
Browse