Using new WP_Query in shortcode in a custom field causes the main post content to not display

You’re using get_posts which returns an array of posts, which you can loop through using foreach, but not the_loop. Try this version of the code:

function ia_news_display_test($atts){ // [ia_news cat="category" num='numbertodisplay']

extract(shortcode_atts(array(
        'cat' => 'any',
        'num' => '2',
        ), $atts));

$dirloop = new WP_Query( array( 
    'post_type' => 'ia_news', 
    'category_name' => $cat,
    'posts_per_page' => $num,
    'orderby' => 'menu_order',
    'order' => 'ASC'
));

    if ($dirloop->have_posts())
    {
        $content = "<ul class="ia_news_list">\n";
        while ( $dirloop->have_posts() ) : $dirloop->the_post();

            $file_id = get_post_meta(get_the_id(), "upload_file", true);
            $file_begin = get_post_meta(get_the_id(), "begin_date", true);
            $file_end = get_post_meta(get_the_id(), "end_date", true);

            if ('' != $file_end)
            {
                $file_end = " to ".$file_end;
            }
            $file_url = wp_get_attachment_url($file_id);

            if ('' != $file_url) 
            {   
                //CHECK FOR EXISTENCE OF FILE URL
                $content .= "<li class="ia_nl"><a href="".$file_url."">".$cat." ".$file_begin.$file_end."</a></li>\n";
            }
        endwhile;
        $content .= "</ul>\n";
    } 
    else
    { 
        $content = "nothing";
    }
    wp_reset_postdata();

    return $content;
}

add_shortcode('ia_news_test', 'ia_news_display_test');

Also, if you’re custom field name is upload_file, then try using get_post_meta instead of taking a long cut as you’re code does.

Leave a Comment