2

can anybody help me understand why I get the followint error:

Object of class stdClass could not be converted to string in... (the error is pointing to the line with implode(), see below)

when I run the following function?

  function selectFullArticle () {

    global $wpdb;

  $id=get_the_ID();


      $webPageArticle = $wpdb->get_results( "SELECT post_content_long FROM $wpdb->posts WHERE ID=$id" );

 $webPageArticle= implode(" ",$webPageArticle);
 return $webPageArticle;

}

My aim is to return a string and not an array.

Maybe the array returned from a SELECT must be treated in a different way?

Thanks in advance,

Marina


Thanks for your answers. I am trying to display a webpage that I downloaded from the web and saved it in a wordpress database, and not a post.

Both $webPageArticle = $wpdb->get_results( "SELECT post_content_long FROM $wpdb->posts WHERE ID=$id",ARRAY_N); and $webPageArticle = $wpdb->get_results( "SELECT post_content_long FROM $wpdb->posts WHERE ID=$id",ARRAY_A);

work well and implode() does not complain anymore. However, I do not get a real string, because the statement "echo $webPageArticle;" visualizes the word "Array" on the screen. T

how come?

marina

1
  • $wpbd->get_result() seems to return an object, aren't there an analogue function that returns an array instead? Check in WP Codex Commented Oct 11, 2011 at 10:05

2 Answers 2

2

As read in Codex, you can pass an additional second parameter to get_result() to allow it to return an array instead of an object

 <?php $wpdb->get_results('query',OBJECT_K); ?> 

returns an associative array you can then manipulate.

Reference:

OBJECT - result will be output as a numerically indexed array of row objects.
OBJECT_K - result will be output as an associative array of row objects, using first column's values as keys (duplicates will be discarded).
ARRAY_A - result will be output as an numerically indexed array of associative arrays, using column names as keys.
ARRAY_N - result will be output as a numerically indexed array of numerically indexed arrays
Sign up to request clarification or add additional context in comments.

Comments

0
function selectFullArticle () {
    $id=get_the_ID();
    $webPageArticle = get_post($id, ARRAY_A);
    $webPageArticle= implode(" ", $webPageArticle);
    return $webPageArticle;
}

If I figured it right and you want the post content only, use this:

function selectFullArticle () {
    $id=get_the_ID();
    $webPageArticle = get_post($id);
    return $webPageArticle->post_content;
}

2 Comments

for your answers. I am trying to display a webpage that I downloaded from the web and saved it in a wordpress database, and not a post. Both $webPageArticle = $wpdb->get_results( "SELECT post_content_long FROM $wpdb->posts WHERE ID=$id",ARRAY_N); and $webPageArticle = $wpdb->get_results( "SELECT post_content_long FROM $wpdb->posts WHERE ID=$id",ARRAY_A); work well and implode() does not complain anymore. However, I do not get a real string, because the statement "echo $webPageArticle;" visualizes the word "Array" on the screen. T how come?
@MarinaSantini $webPageArticle, is not a string, as mentioned in the answer, it is an array.. instead of echo, do a print_r($webPageArticle), and you will see the array content.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.