• Resolved isaacbenh

    (@isaacbenh)


    I have a calendar and when the user clicks on a date I send a call to the API to load posts from that date. No matter what I do I can’t render the posts with the loop.
    Here is what I have:

    function get_events(WP_REST_Request $request)
    {
    
        //ob_start();
        $paged = 1;//(get_query_var('paged')) ? get_query_var('paged') : 1;
        $date = '';
        $event = "";
        $event_date = $request["event_date"];
    
        $args = array(
            'post_type'      => 'event',
            'posts_per_page' => 3,
            'paged'          => $paged,
            'meta_key'       => 'event_date',
            'meta_value'     => $event_date,
            'meta_compare'   => '>=',
            'orderby'        => 'meta_value',
            'order'          => 'ASC'
        );
        //I made sure that there are values here
        $the_query = new WP_Query( $args );
    
        ob_start();
    
        get_template_part('events_template');
    
        $content = ob_get_contents();
        ob_get_clean();
        return $content;
    }

    Here is my events_template:

    <div class="posts-container">
        <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
            <div>
                <?php
                    //Here we group events by event_date and add the date at the top.
                    $event_date =  get_post_meta( $post->ID, 'event_date', true );
                    if ( $event_date != $date )
                    {
                        $event_date_formatted = date( 'l, F jS, Y', strtotime( $event_date ) );
                        echo "<p class='page-header'><strong>$event_date_formatted</strong></p>";
                        $date = $event_date;
                    }
    
                ?>
                <div class="event ath-card">
                    <h4><?php the_title(); ?></h4>
                    <p><?php echo get_post_meta( $post->ID, 'event_location', true ) ?></p>
                    <p><?php echo get_post_meta( $post->ID, 'event_details', true ) ?></p>
                </div>
            </div>
        <?php endwhile; endif; ?>
    </div>
    
    <div class="loader text-center hidden">
        <i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i>
    </div>
    
    <div class="div pagination-container text-center">
        <?php
            next_posts_link( '<button class="ath-btn ath-btn-info">LOAD MORE</button>', $the_query->max_num_pages );
        ?>
    </div>

    It renders all the content that is not inside the if/while statements.

Viewing 1 replies (of 1 total)
  • Thread Starter isaacbenh

    (@isaacbenh)

    I think I solved it, but now I have a problem with the “next_posts_link”.
    I want it to be based on events page, but it’s based on the API URL which is bad:

    <a href="https://localhost:8888/water/wp-json/events/v1/events/page/2/?event_date=2016-08-25" ><button class="ath-btn ath-btn-info">LOAD MORE</button></a></div>

    This is the structure I need:

    href="//localhost:8888/water/events/page/2/"

    Any ideas how to solve this?

Viewing 1 replies (of 1 total)
  • The topic ‘How to render posts via AJAX’ is closed to new replies.