WordPress- Get Posts by Custom Field

I’m trying to make a WordPress plugin for my blog that scans for posts that contain the custom field _videoembed. I’ve got everything made and it activates correctly but I receive a PHP error when I open up a page to test it out:

Fatal error: Call to a member function get_results() on a non-object in .../wp-content/plugins/youtubesubscription/videos.php on line 26.

Read More

Does anyone know enough about PHP to help me out? Here’s my plugin code (pasted on pastebin because of size):
http://pastebin.com/uaEWjTn2

EDIT 1

I’m no longer getting any errors after inserting global $wpdb; but now nothing’s showing up. Here’s my updated code: http://pastebin.com/R2ZuEknY. Note, this code also incorporates auto YouTube thumbnails, which were obtained from a function that trims YouTube URLs to the IDs (link).

EDIT 2

Got it working, turns out all I needed to do was insert ‘_videoembed’ as the ‘meta_key’ argument for a wp_query. Here’s my working code below:

<?php
    $args = array(
        'meta_key' => '_videoembed',
        'post_status'     => 'publish',
        'posts_per_page'    => '' . $number . '',
        'order'    => 'date'
    );
    query_posts( $args );

    while ( have_posts() ) : the_post(); ?>
        <?php 
        global $post;
        $VIDEOID = ytvideoID($post->ID); 
        ?>
        <li onClick="window.location.href='<?php the_permalink(); ?>'">

            <?php
                global $post;
                $videoId = ytvideoID($post->ID);
                $videoInfo = parseVideoEntry($videoId);
                echo '<a href="'.get_permalink().'">';
                    echo '<div class="image">';
                        echo '<img src="http://img.youtube.com/vi/'.$VIDEOID.'/default.jpg">';
                        echo '<div id="time" style="position:absolute;z-index:9;bottom:2px;right:2px;font-size:10px;color:#fff;background:#000;padding:0px 2px;-webkit-border-radius: 4px;-moz-border-radius: 4px;border-radius: 4px;opacity:0.75;">'.hms($videoInfo->length).'</div>';
                    echo '</div>';
                echo '<h4>'.get_the_title().'</h4>';
                echo '<div id="description">';
                    echo '<div id"views"><h3>'.number_format($videoInfo->viewCount).' Views</h3></div>';
                    echo '<div class="singleauthor"><h3>by '.$videoInfo->author.'</h3></div>';
                echo '</div>';
                echo '</a>';
            ?>
        </li>
    <?php endwhile;

    // Reset Query
    wp_reset_query();

    ?>

Related posts

Leave a Reply

3 comments