Support » Fixing WordPress » Fatal Error : Non Object

  • Hi,

    I am trying to run a script, mentioned below, but keep getting the following error:

    Fatal error: Call to a member function get_results() on a non-object
    CAN SOMEONE please help with this error: I am at a lost and have had no response in posting this twice before.

    thanks
    Script is:

    <div id=”content” class=”narrowcolumn”>

    <?php
    global $wpdb;
    $querystr = “
    SELECT wposts.*
    FROM $wpdb->posts wposts, wp_named_users
    WHERE wposts.ID = wp_named_users.post_id
    AND wp_named_users.user_id = ‘$current_user->ID’
    AND wposts.post_type = ‘post’
    AND wposts.post_status = ‘publish’
    AND wposts.post_date < NOW()
    ORDER BY wposts.post_date DESC
    “;

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

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

    <div class=”post” id=”post-<?php the_ID(); ?>”>
    <h2>” rel=”bookmark” title=”Permanent Link to <?php the_title(); ?>”>
    <?php the_title(); ?>
    </h2>
    <small><?php the_time(‘F jS, Y’) ?> <!– by <?php the_author() ?> –></small>
    <div class=”entry”>
    <?php the_content(‘Read the rest of this entry »’); ?>
    </div>

    <p class=”postmetadata”>Posted in <?php the_category(‘, ‘) ?> | <?php edit_post_link(‘Edit’, ”, ‘ | ‘); ?>
    <?php comments_popup_link(‘No Comments »’, ‘1 Comment »’, ‘% Comments »’); ?></p>
    </div>
    <?php endforeach; ?>

    <?php else : ?>
    <h2 class=”center”>Not Found</h2>
    <p class=”center”>Sorry, but you are looking for something that isn’t here.</p>
    <?php include (TEMPLATEPATH . “/searchform.php”); ?>
    <?php endif; ?>

    </div>

Viewing 15 replies - 1 through 15 (of 15 total)
  • Where is the script? You shouldn’t get a ‘non-object’ error unless you are calling the script in a circumstance where the $wpdb class has not been loaded, like somewhere outside of the WP framework.

    thanks–this is driving me crazy 🙁

    not outside the wp framework

    i have defined the ‘global’ functions, but still not letting me run this.

    Script is in an ‘include file’ which I have ‘required’ in to a wp page template.

    All the help I could get would be appreciated!!

    SAm

    If I understand what you are doing, it should work. It works for me. What does the rest of your code look like– the part that includes the file?

    Apljdi-thanks very much for the response. I have modified the script a little as appears below and inserted it directly into the wordpress page template; and it now does not return an error message, but goes straight to the ‘else’, and does not return the content of the table that I am looking for, i.e the posts attributed to the user concerned.

    All I am trying to do is reproduce the relevant posts on my user’s home page, rather than in the wp category template.

    the balance of the script is really then the wordpress page template using the ‘thematic’ theme. Is it a problem perhaps with the current loop on that page: full script of that page appears below:

    Any suggestions appreciated

    thks

    Sam

    <?php get_header() ?>

    <div id=”container”>
    <div id=”content”>
    <?php
    echo(‘<p class=”loginmessage”>You are logged in as ‘ . $current_user->user_login . ‘</p>’);
    ?>

    <?php get_sidebar(‘page-top’) ?>

    <?php the_post() ?>

    <div id=”post-<?php the_ID(); ?>” class=”<?php thematic_post_class() ?>”>
    <?php thematic_postheader(); ?>
    <div class=”entry-content”>

    <?php the_content() ?>

    <?php wp_link_pages(“\t\t\t\t\t<div class=’page-link’>”.__(‘Pages: ‘, ‘thematic’), “</div>\n”, ‘number’); ?>

    <?php edit_post_link(__(‘Edit’, ‘thematic’),'<span class=”edit-link”>’,'</span>’) ?>

    </div>
    </div><!– .post –>

    <?php if ( get_post_custom_values(‘comments’) ) thematic_comments_template() // Add a key+value of “comments” to enable comments on this page ?>

    <div>
    <?php
    $querystr = “
    SELECT wpposts.*
    FROM $wpdb->posts wpposts, $wpdb->named_users wp_named_users
    WHERE wpposts.ID = wp_named_users.post_id
    AND wp_named_users.user_id = ‘$current_user->ID’
    AND wpposts.post_type = ‘post’
    AND wpposts.post_status = ‘publish’
    AND wpposts.post_date < NOW()
    ORDER BY wpposts.post_date DESC
    “;

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

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

    <div class=”post” id=”post-<?php the_ID(); ?>”>
    <h2>” rel=”bookmark” title=”Permanent Link to <?php the_title(); ?>”>
    <?php the_title(); ?>
    </h2>
    <small><?php the_time(‘F jS, Y’) ?> <!– by <?php the_author() ?> –></small>
    <div class=”entry”>
    <?php the_content(‘Read the rest of this entry »’); ?>
    </div>

    <p class=”postmetadata”>Posted in <?php the_category(‘, ‘) ?> | <?php edit_post_link(‘Edit’, ”, ‘ | ‘); ?>
    <?php comments_popup_link(‘No Comments »’, ‘1 Comment »’, ‘% Comments »’); ?></p>
    </div>
    <?php endforeach; ?>

    <?php else : ?>
    <h2 class=”center”>Not Found</h2>
    <p class=”center”>Sorry, but you are looking for something that isn’t here.</p>
    <?php include (TEMPLATEPATH . “/searchform.php”); ?>
    <?php endif; ?>

    </div>

    I have modified the script a little as appears below and inserted it directly into the wordpress page template…

    So did you paste your code into the editor? That doesn’t work. PHP doesn’t execute unless you are running something like the PhpExec plugin.

    Put wpdb->show_errors(); somewhere before your query and var_dump($pageposts); right after you set the variable. If the else always fires then it stands to reason that there is something wrong with the $pageposts variable.

    Thanks–no not through the editor; I work through my ftp client and updated the file. this should work and is very frustrating. could it be because there is an existing loop on that page? i am running shortcode for a custom profile plugin through the editor to generate the user page.. will try the error code to see what is going on and report back.

    thks
    Sam

    hi–I added in the additional code and am getting the following error:

    Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /home/turtle64/public_html/virtuallawdirect.com/wp-content/themes/thematic/page_profile.php on line 62.

    Not surewhere to go from here and any help appreciated.

    thks

    Sam

    …just add: I cant see why the “->” would give rise to an error–very strange

    I have played around with the code and get the following result on the page (no error message):

    array(0) { }
    Not Found

    Sorry, but you are looking for something that isn’t here.

    the reference to the T Object Operator was to the wpdb->show_errors(); had inserted. not clear at all why this script is not reproducing the posts I require: is there an easier way of achieving what I need through a feed or something similar:

    Completley lost 🙁

    It has to be your query then. Echo your query string.

    thanks–bu unfortunately no success

    <?php
    echo $querystr;
    ?>

    produces

    SELECT wpposts.* FROM wp_posts wpposts, wp_named_users WHERE wpposts.ID = wp_named_users.post_id AND wp_named_users.user_id = ‘1’ AND wpposts.post_type = ‘post’ AND wpposts.post_status = ‘publish’ AND wpposts.post_date < NOW() ORDER BY wpposts.post_date DESC
    Not Found

    Sorry, but you are looking for something that isn’t here.

    not sure where to go from here.

    Sam

    apljdi- thanks for your help with this—-this is likely a DB table issue as it is working for some users in respect of some posts, but not others. not clear why.

    …this is likely a DB table issue…

    Right, which is why I wanted you to echo the string and to check $wpdb->show_errors(). If you aren’t getting an error then there must be something about the structure of the query that isn’t right. Run that string directly on the DB (via PhpMyAdmin or whatever you have available) using a user that works and one that doesn’t. Then start paring down the query, removing elements until it works in both cases. Then, of course, rebuild it, but differently.

    Where does wp_named_users come from? And are you sure that all of your posts (all of the ones you want the query to grab, anyway) have a corresponding user_id in wp_named_users?

    wp_named_users is from a plugin, namedusers or something to that effect. I used it as seems to me to be the only way (other than creating a table from scratch) in getting a user_id and post_id join. But I think the named_users table is defective and will need to rebuild this. I will first go through the process of elimination you suggest and will feed back on where I get to.
    thanks v much for your assistance,

    Sam

    … getting a user_id and post_id join…

    Is it not possible to use the built in ‘Post Author’ functions?

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Fatal Error : Non Object’ is closed to new replies.