I am trying to get a query to echo on the frontend of wordpress from a plugin but haven't had much success. Any and all help would be greatly appreciated.

Below you will find the simple script I am using:

    function getPageIDs()
       include_once($_SERVER['DOCUMENT_ROOT'].'/stage/wp-config.php' );
        global $wpdb;

       $row = $wpdb->get_row( 'SELECT pages FROM schemalocalbusiness WHERE id = 1');

        $pageIDs = $row->pages;

        $pageIDsArray = explode(" ", $pageIDs);

        foreach ($pageIDsArray as $page)

                echo $page;


add_action('wp_footer', 'getPageIDs', 1);

I have tried several different alteration of this script with no luck. Thank you for your help!

This is what the database query returns: this is what the database query returns

  • You wouldn't need to include wp-config.php again. Is the function called at all (add an echo outside the foreach)? Does the SQL return something when you run it against the database manually? Does it return a non-empty value for pages? Did you check that the output really doesn't show up in the source and isn't just hidden by a CSS rule?
    – janh
    Dec 27 '17 at 8:02
  • I have tried not including the wp-config.php within each function I use. However, each time my database queries are unsuccessful. And yes, the query to the database is correct. I also check the source code on the front end to make sure it isn't hidden. I even went as for as to do a simple example: function execute() { if(is_page(6)) { echo '<h1>SUCCESS</h1>'; } echo 'test'; } add_action('wp_footer', 'execute'); and still the same results.... NOTHING! lol Thank you for your advice @janh Dec 27 '17 at 8:20
  • In your minimal example, is "test" outputted at least? In what way are your DB queries unsuccessful if you don't include wp-config.php, do they not return anything, or are they failing completely because there's no DB connection available / $wpdb isn't defined? Does schemalocalbusiness exist in your WP database or are you switching databases on the fly?
    – janh
    Dec 27 '17 at 8:36
  • No, "test" doesn't even work. Without putting in the wp-config.php it doesn't return anything. Everything works with the query. Just not the add_action to the footer of the frontend pages. And yes, schemalocalbusiness does exist in the WP database. I simply cannot get the hook to work.... @janh Dec 27 '17 at 8:40
  • Sounds like the query & its output isn't your problem, but something much more basic. Is this some hybrid setup where WP is being included in another framework? Does your theme call wp_footer()? Is the document completed (no error / exit that stops processing before wp_footer() is executed)?
    – janh
    Dec 27 '17 at 8:46

Once I got the syntax correct, @janh was correct. wp_footer has to be called BEFORE all_admin_notices. I was trying to call in wp_footer from all_admin_notices. Simple but frustrating! Thank you @jahn for all of your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.