WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Trouble getting $wpdb to output (6 posts)

  1. FMarion
    Member
    Posted 2 years ago #

    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));
    print_r($sql_query);

    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?

  2. stvwlf
    Member
    Posted 2 years ago #

    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: http://digwp.com/2011/05/loops/

    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.

  3. FMarion
    Member
    Posted 2 years ago #

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

    http://pastebin.com/vCdDRHZj

    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.

  4. stvwlf
    Member
    Posted 2 years ago #

    See this page:
    http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query

    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.

  5. FMarion
    Member
    Posted 2 years ago #

    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!

  6. stvwlf
    Member
    Posted 2 years ago #

    You are welcome. Glad it was helpful

Topic Closed

This topic has been closed to new replies.

About this Topic