Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've got a custom post type of files I'm trying to list using a new WP_Query.

It works fine when I add the shortcode within the main post content. However, when I add the shortcode to a custom field (which I have set to display on the page along with the main content as a secondary "column"), the main content disappears!

If I comment out the "while...have_posts()" and "endwhile" lines, the main content displays; however, even after commenting out every other line, the main content disappears unless I also remove the "while".

I am pretty certain it has to do with using WP_Query (somehow it hijacks the loop), even though it shouldn't.

Is there something I'm missing?

Here's the shortcode:

   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();
        $custom = get_post_custom($post->ID);
        $file_id = $custom["upload_file"][0];
        $file_begin = $custom["begin_date"][0];
        $file_end = $custom["end_date"][0];
        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"; }

return $content;

}

share|improve this question
    
Edited with the actual code I was attempting to use...sheesh! –  jfacemyer Mar 4 '12 at 1:31

3 Answers 3

up vote 4 down vote accepted

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.

share|improve this answer
    
YIKES! I was tired and put in the version of the code I was playing around with. I actually was using WP_Query, which caused the problem (I believe), so I was starting to use the get_posts function. I'll try to paste in your version to see what happens. –  jfacemyer Mar 4 '12 at 1:24
    
SWEET! I wonder if it was using get_post_meta...it appears that was all that was really changed from my original code. You're the bomb. I'd give you a bump up if I could ^^ –  jfacemyer Mar 4 '12 at 1:51
    
Awesome! Accepting the answer would be better than a bump ;) –  Rutwick Gangurde Mar 5 '12 at 4:00
    
:) Still learning :) –  jfacemyer Mar 9 '12 at 1:50

You should use wp_reset_postdata() before you return out of the function. Similar to wp_reset_query(), but restores the global $post instead of $wp_the_query.

share|improve this answer

Try to use wp_reset_query() after the loop. That should reset your query. Let me know it works or not.

share|improve this answer
1  
The author’s code doesn’t change the main query. Why should a reset do anything useful? –  toscho Mar 3 '12 at 7:34
    
+1 @toscho. If he was using query_posts, wp_reset_query() was the solution. –  Rutwick Gangurde Mar 3 '12 at 8:06
    
I believe this is true using new wp_query as well, since it sets up a separate loop, correct? In any case, I had already tried this (just to be certain) with no success. –  jfacemyer Mar 4 '12 at 1:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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