LoneWolfPR
LoneWolfPR

Reputation: 4100

WordPress: WP_Query in functions.php returns empty query (for a custom shortcode)

I'm writing a custom shortcode for a childtheme that I've dropped into my child themes functions.php. Here's the code:

function get_featured_video(){
$video_query = new WP_Query('category_name=videos&order=ASC');
$videoPath = "/";
$videoText =  "";
while ($video_query->have_posts()){
    $video_query->the_post();
    $featured = get_post_meta($post->ID, "vid_feature", $single = true);
    if(strtolower($featured)=='yes'){
        $videoPath = get_permalink($post->ID);
        $content = strip_tags($post->post_content);
        $contentArr = str_word_count($content,1);
        if(count($contentArr>50)){
            $videoText = join(" ",array_slice($contentArr,0,50));
            $videoText .= " <a href='$link'>&lt;read more&gt;</a>";
        } else {
            $videoText = $content;
        }
        break;
    }
}
$returnStr = "<h1><a href='$videoPath'>You've Got to See This!</a></h1>\n";
$returnStr .= $videoText;
return $returnStr;
}

add_shortcode('getfeaturedvideo','get_featured_video');

The problem I'm having is that it's returning a blank query. I know there's a post in the videos category. I've never used the WP_Query inside functions.php. Is there a different method I need to use?

Upvotes: 1

Views: 5085

Answers (1)

CookiesForDevo
CookiesForDevo

Reputation: 721

At the top of the function try declaring:

global $post;

Upvotes: 5

Related Questions