• Resolved David Gard

    (@duck_boy)


    Hey all,

    I am using the code below –
    $contactLink = $wpdb->get_var('SELECT post_name FROM wp_posts WHERE post_title="' . $contact->name . '" AND post_status="publish"');
    – to try and pull the details of a Post so that I can link to it, but it’s trhowing up a fatal error –

    Fatal error: Call to a member function get_var() on a non-object in … on line …

    I’m not sure where I am going wrong here, so any help would be appriciated.

    Thanks.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Use $wpdb->get_row or $wpdb->get_results instead of $wpdb->get_var.

    Thread Starter David Gard

    (@duck_boy)

    Hey Michael,

    Thanks for the advice, but all give the same result unfortunatly.

    If it helps, here is the entire function, don’t know if you can spot an obvious error in the there at all?

    function get_postContacts() {
    // Get Post Contacts function
    // Displays the name of contact for an article at the top of the article and links to their profile
    	$contacts = get_the_terms( $post->ID , 'contact' );
    
    	if(!empty($contacts)) {
    		echo '<div class="floatRight ourPeopleBox">';
    			echo '<div class="rightColItem rc_brochure rc_title">';
    				echo '<p><strong>For more information contact</strong></p>';
    			echo '</div>';
    			echo '<ul>';
    				foreach($contacts as $contact){
    					$contactLink = $wpdb->get_row('SELECT post_name FROM wp_posts WHERE post_title="' . $contact->name . '" AND post_status="publish"');
    					echo '<li><a href="https://www.dynedrewett.com/' . $contactLink . '">' . $contact->name . '</a></li>';
    					//wp_reset_query();
    				}
    			echo '</ul>';
    		echo '</div>';
    	}
    }

    And, agian if it may be of use, here is the output for ‘print_r($contact);’ for one of the contacts.

    stdClass Object (
    [term_id] => 100
    [name] => John Smith
    [slug] => john-smith
    [term_group] => 0
    [term_taxonomy_id] => 116
    [taxonomy] => contact
    [description] =>
    [parent] => 0
    [count] => 4
    [object_id] => 722
    )

    After this line:

    $contactLink = $wpdb->get_row('SELECT post_name FROM wp_posts WHERE post_title="' . $contact->name . '" AND post_status="publish"');

    use
    echo "<pre>"; print_r($contactLink); echo "</pre>";
    to ascertain your variable names.

    It should be something like $contactLink[0]->post_name

    Thread Starter David Gard

    (@duck_boy)

    Hmm, still getting an error. I have shortened the function to below for now, to eliminate anything unnecessary, but still getting the Fatal Error for get_row().

    It is printing $contact and then erroring.

    function get_postContacts() {
    	$contacts = get_the_terms( $post->ID , 'contact' );
    	if(!empty($contacts)) {
    		foreach($contacts as $contact){
    			"<pre>"; print_r($contact); echo "</pre>";
    			$contactLink = $wpdb->get_row('SELECT post_name FROM wp_posts WHERE post_title="' . $contact->name . '" AND post_status="publish"');
    			"<pre>"; print_r($contactLink); echo "</pre>";
    		}
    	}
    }

    Thanks.

    Thread Starter David Gard

    (@duck_boy)

    Oh dear, i have found the problem…

    one little line missing from the Function –
    global $wpdb;

    Thanks for your time, I’ll know to include that next time!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘$wpdb->get_var Fatal Errro’ is closed to new replies.