Advertisement
Guest User

Wordpress Query outside of Wordpress

a guest
Apr 21st, 2010
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.08 KB | None | 0 0
  1. <?php
  2. // ...Call the database connection settings
  3. require( path to /wp-config.php );
  4.  
  5. // ...Connect to WP database
  6. $dbc = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
  7. if ( !$dbc ) {
  8.     die( 'Not Connected: ' . mysql_error());
  9. }
  10. // Select the database
  11. $db = mysql_select_db(DB_NAME);
  12. if (!$db) {
  13.     echo "There is no database: " . $db;
  14. }
  15.  
  16. // ...Formulate the query
  17. $query = "
  18.    SELECT *
  19.    FROM `wp_posts`
  20.    WHERE `post_status` = 'publish'
  21.    AND `post_password` = ''
  22.    AND `post_type` = 'post'
  23.    ";
  24.  
  25. // ...Perform the query
  26. $result = mysql_query( $query );
  27.  
  28. // ...Check results of the query and terminate the script if invalid results
  29. if ( !$result ) {
  30.     $message = '<p>Invalid query.</p>' . "\n";
  31.     $message .= '<p>Whole query: ' . $query ."</p> \n";
  32.     die ( $message );
  33. }
  34.  
  35. // Init a variable for the number of rows of results
  36. $num_rows = mysql_num_rows( $result );
  37.  
  38. // Print the number of posts
  39. echo "$num_rows Posts";
  40.  
  41. // Free the resources associated with the result set
  42. if ( $result ) {
  43.     mysql_free_result( $result );
  44.     mysql_close();
  45. }
  46. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement