1

I am trying to pull some data from a remote wordpress database. Here is the code I have:

$server = "my server code";
$user = "my username";
$password = "my password";
$db = "mydb";
$con = mysql_connect($server,$user,$password); 
if (!$con) {
    die("database connection error");

} else {

mysql_select_db($db, $con);

function dp_get_user_liked_posts($user_id, $fields = '') {

    global $wpdb;

    $tempDB = $wpdb;

$wpdb = new wpdb($user, $password, $db, $server);

    // Get user settings
    $settings = get_option('dp_post_likes');

        $login_required = !empty($settings['login_required']) ? true : false;

            if($login_required && !is_user_logged_in()) {

                echo '<p class="must-login">'.sprintf(__('You must <a href="%1$s">register</a> and <a href="%2$s">login</a> to view your liked videos.', 'dp'),  site_url('wp-login.php?action=register', 'login'), wp_login_url()).'</p>';

            } else {

    $settings = get_option('dp_post_likes');
    $user = get_current_user_id();
    $user_id = 2;
    $likes=query_posts(array(
                            'post_type' => 'post',
                            'ignore_sticky_posts' => true,
                            'filter_type' => 'user_liked',
                            'filter_user' => $user_id
                        ));

                global $wp_query;
                 $likes= array();
                 while (have_posts()) : 
                            $likes[]=the_post() ;
                        endwhile;

}}}  
        mysql_close($con);

$wpdb = $tempDB;

but it is not display $likes. What is did wrong?

1 Answer 1

3

Ok. There are few things wrong here.

query_posts isn't mean to be used that way so don't. Try the same code with $likes=new WP_Query(array(... instead.

$likes=new WP_Query(array(
                        'post_type' => 'post',
                        'ignore_sticky_posts' => true,
                        'filter_type' => 'user_liked',
                        'filter_user' => $user_id
                    ));

That is problem one. Then even if $likes were set correctly you'd clobber it.

$likes= array();

Why would you expect to have data in $likes after you've erased all of the data? Don't do that. Remove that line.

Now this:

while (have_posts()) : 
    $likes[]=the_post() ;
endwhile;

...is pointless. After you run the query $likes is already a collection of posts. Run var_dump($likes) and you will see what I mean. There is no need to loop over it just to do what is already been done. Just return $likes or loop over it and do something, like echo content.

Besides, using the_post like that wouldn't work. That isn't what the the_posts method does.

If, on the other hand, you are trying to extract, for example, the post content you'd want...

while ($likes->have_posts()) : 
    $likes->the_post();
    $cont[] = $post->post_content;
endwhile;

I'd really suggest you take some time to read about creating loops.

You can do about the same with get_posts but the loops are a little different.

Also, why are you mixing PHP's mysql_* functions with $wpdb? And why are you using global $wpdb only to then clobber it? Why don't you just use a different name for your database instance?

Hopefully that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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