WordPress.org

Ready to get started?Download WordPress

Forums

$wpdb->get_results Stops Page Load (11 posts)

  1. spyke01
    Member
    Posted 3 years ago #

    Hey guys, I'm writing a plugin to pull posts from the DB based on meta_keys and then print posts based on the meta value someone clicks. The issue is that for some reason when the system hits $wpdb->get_results the rest of the page (everything after the calling shortcode) doesn't display. I've verified the query that is generated pulls results and there's no issues in it but can't find whats happening.

    Here's the offending function:

    [Large code excerpt removed by moderator per forum rules. Please use the pastebin for all large code excerpts. It works better anyway.]

  2. spyke01
    Member
    Posted 3 years ago #

  3. spyke01
    Member
    Posted 3 years ago #

    After further investigation it appears that pulling from the postmeta table is whats causing the issue, the same code can pull from any other table and using wp_postmeta instead of $wpdb->postmeta causes the same issue. The interesting thing is that if I make the query invalid by changing it to postmeta the query of course fails but the page loads.

  4. Mark / t31os
    Moderator
    Posted 3 years ago #

    Works perfectly fine for me, some simple test code.

    $querystr = "SELECT * FROM $wpdb->postmeta pm JOIN $wpdb->posts p ON p.ID = pm.post_id";
    $myPosts = $wpdb->get_results($querystr, OBJECT);
    print 'hello world!';
    var_dump( $myPosts );

    I see "hello world" and the var_dump() result just fine.

    What does this line output for you?

    print $querystr; // This line prints

    It would be my guess/assumption you're not forming that query string correctly.

    NOTE: You don't need those backticks around the table names.

  5. spyke01
    Member
    Posted 3 years ago #

    Looks like its the WP site I'm testing on, not sure its the theme or something else but with a base WP install everything works so something on my end.

    Thanks!

  6. Mark / t31os
    Moderator
    Posted 3 years ago #

    EDIT: Nevermind, i see what you're doing, checked the pastebin again, that's clearly a shortcode function.

    NOTE: Your get_results call doesn't need to declare the return type(ie. object), object is default.

    A bit stumped as to the cause of the problem though.

    Does the query work if you remove the query string and perform a generic query like my sample one above?

  7. spyke01
    Member
    Posted 3 years ago #

    Nope even very basic queries fail, however if i comment the get_results line everything loads fine, very strange issue.

  8. Mark / t31os
    Moderator
    Posted 3 years ago #

    Maybe you're not getting access to $wpdb when you should be..

    What does a var_dump() or print_r() on $wpdb produce?

  9. spyke01
    Member
    Posted 3 years ago #

    It actually dumps out a ton of info from that variable leading me to believe the variable is valid but still doesn't dies at the same spot.

  10. Mark / t31os
    Moderator
    Posted 3 years ago #

    Well that's odd i must admit, can't think why you'd have the problem with get_results.

    Is the site you used for testing where the code works also running the same version of WordPress? (3.0.3 is it?)..

  11. spyke01
    Member
    Posted 3 years ago #

    Sorry looks like I posted the wrong version, the test site is 3.0.3 and the site its failing on is 3.0.4

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.