2

I am trying to access my Wordpress database from a file that is outside of the theme files. However, I can't directly use the wp-config information because they change every day. This results in the necessity of the global $wpdb variable.

Could anyone provide some assistance in the way of accessing the database?

Here's my code trying to gain access :

<?php

 define('WP_USE_THEMES', false);
require('../wp-blog-header.php');


 global $wpdb;

?>

...

<?php

if(isset($_POST['post_variable'])) {
$selected_post = $_POST['post_variable'];
}

elseif(isset($_GET['post_variable'])) {
$selected_post = $_GET['post_variable'];
}


$sql1 = "SELECT * FROM wp_posts WHERE post_status = 'publish' AND post_type = 'home-messages' AND ID = '$selected_post'";
$result1 = mysql_query($sql1) or die(mysql_error());
while ($row1 = mysql_fetch_assoc($result1)) {
$display_post_content = $row1['post_content'];
$display_post_title = $row1['post_title'];

}
?>
Seth Spivey
  • 347
  • 2
  • 4
  • 22
  • just load wp and use wpdb or if you really must require the config file and use the settings. – David Nov 03 '16 at 01:02
  • Possible duplicate of [Using WPDB in standalone script?](https://stackoverflow.com/questions/5306612/using-wpdb-in-standalone-script) – chris Sep 24 '18 at 14:36

1 Answers1

0

I think you need to use mysql_connect, with the global $wpdb use the WordPress sql method.

Benoti
  • 2,170
  • 2
  • 14
  • 15