My get_results query not returning any results

Anyone got any clues please?
[php]

<?php //Assign users to studies as authors global $wpdb; echo "Started"; $wpdb->show_errors(); // First get the profiles $result = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type='name_profile'"); print_r ($result); foreach ($result as $results){ $study_id = $results->id; echo "Study ID= ". $study_id; $study_name = $results->post_title; echo $study_name; /*$result1 = $wpdb->get_row("SELECT * FROM $wpdb->variants WHERE study = $study_name"); $author = $result1->number; $result2 = $wpdb->get_row("SELECT id FROM $wpdb->users WHERE user_login = $author"); $author = $result2->id; echo $author; $my_post = array( 'ID' => $study_id, 'post_author' => $author ); // Update the post into the database wp_update_post( $my_post );*/ echo "Updated"; } ?>

[/php]

The echo statements as well as the commented out code are for testing and showing no results.

Try doing this in you code, so we can see the SQL that is being executed. Once you get the SQL run it directly in your database and see if you get results

[php]echo “SELECT * FROM $wpdb->posts WHERE post_type=‘name_profile’”[/php]

The query runs fine in phpmyadmin and returns the 9 rows that I expect.

I just can’t get it to work on the live site from a php page.

Did you try wrapping it in curly braces?

[php]$result = $wpdb->get_results(“SELECT * FROM {$wpdb->posts} WHERE post_type=‘name_profile’”);[/php]

I have and still no results from the query.

Just Curious try this…

[php]$result = $wpdb->get_results("SELECT * FROM " . $wpdb->base_prefix . “posts WHERE post_type=‘name_profile’”);[/php]

Or put the table name in directly… Assuming your prefix is wp_

[php]$result = $wpdb->get_results(“SELECT * FROM wp_posts WHERE post_type=‘name_profile’”);[/php]

and echo this, curious what it shows you.

[php]echo $wpdb->posts[/php]

Thanks for your suggestions, but I was getting nowhere. >:(

I gave up in the end and just coded a stand alone page with mysqli rather that Wordpress db commands and it worked perfectly. As this is a one off exercise this will do. :smiley:

Yeah, wordpress is PITA

Sponsor our Newsletter | Privacy Policy | Terms of Service