Support » Plugins » Hacks » $wpdb->get_results Stops Page Load

  • 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.]

Viewing 10 replies - 1 through 10 (of 10 total)
  • 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.

    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.

    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.


    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?

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

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

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

    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.

    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?)..

    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

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘$wpdb->get_results Stops Page Load’ is closed to new replies.