wp-posts-to-posts icon indicating copy to clipboard operation
wp-posts-to-posts copied to clipboard

Can't fetch value in custom query

Open vajrasar opened this issue 10 years ago • 0 comments

Need to fetch URL of 'gallery' connected to 'event'. Using the following code on Event Archive page and not able to get what I want. What I mean is that the first post only has a relation between event-gallery and no other event has. But, the URL is being shown at desired place for all other posts but not the first post.

I know am making some rookie/obvious mistake. Please let me know if you see something.

<?php
        global $post;

        $currentdate = date("m/d/Y 00:00:00");
        $cd = DateTime::createFromFormat( 'm/d/Y H:i:s', $currentdate );
        $currentdate = $cd->getTimestamp();

        $args = array(
            'post_type' => 'event',
            'posts_per_page'=> '10',
            'orderby' => 'meta_value',
            'meta_key' => 'cmb2_event_edate',
            'meta_compare' => '<',
            'meta_value' => $currentdate,
            'order' => 'ASC',
        );

        $loop = new WP_Query( $args );

        // p2p_type( 'gallery_to_event' )->each_connected( $loop, array(), 'gallery' );
        p2p_type( 'gallery_to_event' )->each_connected( $loop );

        ?>
        <section id="news-gallery">
            <div class="container">
                <div class="row">

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

                            $city = get_post_meta( $post->ID, 'cmb2_event_city', true );
                            $venue = get_post_meta( $post->ID, 'cmb2_event_venue', true );
                            $sdate = get_post_meta( $post->ID, 'cmb2_event_sdate', true );
                            $edate = get_post_meta( $post->ID, 'cmb2_event_edate', true );

                            $sdate = date('d F', $sdate);
                            $edate = date('d F', $edate);

                            $temp = $post;

                            foreach ( $post->connected as $post ) {
                                // echo '<pre>';
                                // print_r( $post );
                                // echo '</pre>';
                                setup_postdata( $post );

                                $connected_gallery = $post->guid;
                            }
                            wp_reset_postdata();

                            $post = $temp;

                            ?>
                            <div class="col-xs-12 col-sm-4 col-md-4">
                                <div class="up-event-widget">
                                    <div class="up-event-img">
                                        <?php echo get_the_post_thumbnail( $post->ID, 'full' ); ?>
                                    </div>
                                    <a class="abso-view" href="<?php echo $connected_gallery; ?>">VIEW MORE</a>
                                    <div class="up-event-details">
                                        <h3><?php  echo $city; ?></h3>
                                        <h4><?php echo $sdate . " - " . $edate; ?></h4>
                                        <div class="up-event-address"><?php echo $venue; ?></div>
                                    </div>
                                </div>
                            </div>
                            <?php                        
                        }
                    }
                    ?>

                </div>
            </div>
        </section>

Need to fetch $connected_gallery = $post->guid; to use later in HTML as <a class='abso-view' href='<?php echo $connected_gallery; ?>'>VIEW MORE</a>

vajrasar avatar Jun 22 '15 10:06 vajrasar