Matt
Matt

Reputation: 570

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.

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();

    ?>

Upvotes: 2

Views: 4567

Answers (3)

Liz Eipe C
Liz Eipe C

Reputation: 255

you get the post by custom field using the meta_query.

 $args= array(
    'category_name' => 'courses',
    'orderby'       => 'menu_order',
    'order'         => 'ASC',
    'meta_query' => array(
        array(
            'key'     => 'front_page',
            'value'   => 'yes',
            'compare' => 'LIKE',
        ))

);

$the_query = new WP_Query( $args );

Upvotes: 4

Mark
Mark

Reputation: 3271

You'll have to to add global $wpdb;

try replacing

function mbrecentvids()
{ ?>
<?php

with

function mbrecentvids()
{
    global $wpdb;

Upvotes: 1

rabid_zombie
rabid_zombie

Reputation: 992

It looks like you're missing a semi-colon on line 26.

I see:
$pageposts = $wpdb->get_results($querydetails, OBJECT_K)

Upvotes: 0

Related Questions