Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3235094
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:28:02+00:00 2026-05-17T17:28:02+00:00

I have a php script that I’ve put together from examples on the internet.

  • 0

I have a php script that I’ve put together from examples on the internet.

I’m not very familiar with php and I’m getting an error.

I am trying to get the last post date of my WordPress blog for my program to show when the last date that we put important information for the user on it.

This is my code:

    <?php 

    function lpd_post_date( $date_format, $echo, $modified_date = false )
    {         

        $dbhost = 'xxx';
        $dbuser = 'xxx';
        $dbpass = 'xxx';
        $dbname = 'xxx';

        $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
        mysql_select_db($dbname);

        // include 'config.php';
        // include 'opendb.php';

        global $wpdb;
        $date_col = 'post_date';
        if ( $modified_date ) $date_col = 'post_modified';
        $sql = " SELECT $date_col FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type = 'post' ORDER BY $date_col DESC LIMIT 1 ";
        $mysql_date = $wpdb->get_var( $sql );
        if ( ! $date_format ) $date_format = get_option('date_format');
        $formatted_date = mysql2date( $date_format, $mysql_date );
        echo $formatted_date;        

        mysql_close($conn); 

    }
?>

I am getting this error:

Fatal error: Call to a member function
on a non-object in
/homepages/38/d157689362/htdocs/suburban/updates/last/last.php
on line 21

Anyone able to help me out? I’m lost as to what it is doing on line 21:

$mysql_date = $wpdb->get_var( $sql );

Any help would be greatly appreciated!

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:28:02+00:00Added an answer on May 17, 2026 at 5:28 pm

    This error is indeed because $wpdb is not defined.

    You are better off putting this code in the WordPress functions.php file that WP creates for your active theme (found in ‘wp-content/themes/active_theme_name/functions.php’ – the default theme is ‘default’ if I recall correctly). $wpdb already is defined in this file, and the code you have written should work there (take out the <?php and ?> and just put the function in the file somewhere with the others). You would then call the function you have created either by:

    1. Putting include_once('_path_to_active_theme/functions.php'); at the top of your script and then call the function in your new php file:

      $return_value = lpd_post_date($date_format, $echo, $modified_date);

      //Do whatever we need to do with $return_value

    2. Call the function in your theme UI code – if of course you are trying to put the value lpd_post_date return value in your UI somewhere

    The following may help:

    http://wordpress.org/support/topic/get_var-on-a-non-object-wpdb
    http://codex.wordpress.org/Theme_Development#Functions_File

    It would definitely be a good idea to read that entire Theme_Development 2nd link if you haven’t already.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.