Support » Fixing WordPress » Trouble getting $wpdb to output

  • Resolved FMarion


    I’m having issues outputting the value of a $wpdb query that I’ve written. The SQL is successful. A print_r() will output the correct values, but now I have to output them in a loop (and all the commensurate functions that become available within it), and for the life of me, I can’t seem to get this working. I have called global $wpdb; at the top of my document.

    $sql_get_posts = "
    SELECT *** Big multi-join, multi-criteria but fully functional SQL query ***
    //  Run the query and set it to a variable
    $sql_query = $wpdb->get_results($wpdb->prepare( $sql_get_posts));

    This seems to output my values correctly.

    But $the_search_results = query_posts($sql_query); which in theory feeds my loop does not. I suspect that it is because this is expecting $post data.

    I am using while ( have_posts() ) : the_post(); so that I can use things like get_permalink( $post->ID ) and has_post_thumbnail() and the_post_thumbnail()

    What am I doing wrong? What should I be doing to avail myself of my query results?

Viewing 5 replies - 1 through 5 (of 5 total)
  • What does your loop code look like?

    And, if you are not already doing so, using a custom query rather than query_posts.

    Here’s a good article:

    Use wp_query or get_posts rather than query_posts.

    query_posts used to be the recommended way so older articles use it. But its no longer recommended.

    The following is an example of my query, my loop and example content that I’d like to use:

    I did note that when I went from the typical if (have_posts()) : while (have_posts()) : the_post(); loop to a foreach(){} that a print_r() did return the results, but I can’t seem to get them to be pushed into the desired functions, such as the_post_thumbnail('place_list'); and so on.

    I’ve tried doing $item->have_posts() […etc…], but no good on that either.

    See this page:

    Notice that it goes from this

    $pageposts = $wpdb->get_results($querystr, OBJECT);

    to this

    <?php if ($pageposts): ?>
     <?php global $post; ?>
     <?php foreach ($pageposts as $post): ?>
     <?php setup_postdata($post); ?>
     <div class="post" id="post-<?php the_ID(); ?>">

    in particular these two lines

    <?php foreach ($pageposts as $post): ?>
     <?php setup_postdata($post); ?>

    By assigning the new post in the foreach loop to the global $post, all of the WordPress built-ins associated with wp_query are available.

    The next line setup_postdata($post) lets you use all of the normal WordPress in-loop template tags – the_title(), the_content() etc. with your custom query.

    This only works when you use “…as $post” in your foreach.

    Excellent! This worked beautifully. It took me about three solid days of messing around, practicing GoogleFu and managing content overload. You’ve helped me resolve the issue, both by pointing out the page and pointing the specific places to look at. I really, really appreciate it. Thanks!

    You are welcome. Glad it was helpful

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Trouble getting $wpdb to output’ is closed to new replies.