0

When I try to render a list of recent post type either by using WP functions or WP query, I'm not able to render the custom fields that I have to render bit after.

The content of box with class feat-block ends up empty every time when using one of following commands:

$recent_news_posts = wp_get_recent_posts($news_post_settings); // grab post as array
$recent_news_posts = get_posts($news_post_settings); // grab post as an object
$recent_news_posts = wp_get_archives('postbypost', '5');
$recent_news_posts = $wpdb->get_results($request);

Why would this be?

Here's my code:

<div id="home-right-bottom">
    <div class="news-block home-block">
        <h2>In the News</h2>
        <div class="content">
            <?php

                $news_post_settings = array(
                    'posts_per_page'   => 5,
                    'offset'           => 0,
                    'category'         => '',
                    'category_name'    => '',
                    'orderby'          => 'date',
                    'order'            => 'DESC',
                    'include'          => '',
                    'exclude'          => '',
                    'meta_key'         => '',
                    'meta_value'       => '',
                    'post_type'        => 'post',
                    'post_mime_type'   => '',
                    'post_parent'      => '',
                    'author'           => '',
                    'post_status'      => 'publish',
                    'suppress_filters' => true
                );

                //$recent_news_posts = wp_get_recent_posts($news_post_settings); // grab post as array
                //$recent_news_posts = get_posts($news_post_settings); // grab post as an object
                //$recent_news_posts = wp_get_archives('postbypost', '5');

                global $wpdb;

                $post_limit = 5;
                $request = "SELECT ID, post_title, post_date, guid FROM $wpdb->posts WHERE post_status = 'publish' AND post_type='post' ORDER BY post_date DESC LIMIT $post_limit";
                $recent_news_posts = $wpdb->get_results($request);

                print '<ul class="recent_news">';

                foreach($recent_news_posts as $post) {
                    /*$title = $post->post_title;
                    $date = mysql2date('m.d.y', $post->post_date);
                    $url = $post->guid;*/
                    $title = $post['post_name'];
                    $date = mysql2date('m.d.y', $post['post_date']);
                    $url = $post['guid'];

                    print '<li><span class="date">' . $date . '</span> - <a href="' . $url . '">' . $title . '</a></li>';
                }

                print '</ul>';

            ?>
        </div>
    </div>

This block is not rendering after running command above. If I comment above out, following is working:

    <?php

        //if(get_field('featured_news')):

            $feat = get_field('featured_news');
    ?>

    <div class="feat-block home-block">
        <h2>Featured News</h2>
        <div class="content">
            <?php

                if(!empty($feat[0]['news_link'])) {
                    print '<a href="' . $feat[0]['news_link'] . '">';
                }

                if(!empty($feat[0]['news_image'])) {

                    $news_image_url = $feat[0]['news_image']['url'];
                    $news_image = aq_resize($news_image_url, 300, 180, true);

            ?>
                <img src="<?php echo $news_image; ?>" alt="<?php echo $feat[0]['news_image']['caption']; ?>" class="feat_img">
            <?php
                }
            ?>
            <p><?php
                if(!empty($feat[0]['news_short_description'])) {
                    print $feat[0]['news_short_description'];
                }
           ?></p>

           <?php

            if(!empty($feat[0]['news_link'])) {
                print '</a>';
            }

           ?>
        </div>
    </div>

    <?php

        //endif;

    ?>
</div>

1 Answer 1

1

You overwrite the global $post which is where that function gets the post ID from to fetch your custom field. You need to add wp_reset_postdata() after you run other queries that use $post.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.