Query WordPress database, returning an array of rows This is one of my first attempts at querying the WordPress database for data, and some my first PHP code ever, so please bear that in mind when commenting.
It is supposed find data between two dates, and show it as HTML in a for each loop. How can this be made better? What is the correct way to query a WordPress database?
&lt;?php 
require_once('wp-load.php');
$sd = date('Y-m-d', strtotime(($startdate = filter_input(INPUT_GET, 'startdate'))));
$ed = date('Y-m-d', strtotime(($enddate = filter_input(INPUT_GET, 'enddate'))));
$query = $wpdb-&gt;prepare(&quot;SELECT FirstName, LastName, FROM WHEN HireDate BETWEEN '%s' and '%s'&quot; $sd, $ed);
echo $query; 
$results = $wpdb-&gt;get_results( $query );
?&gt;
If you're working with a custom table you have within your database, which you use for WP, then using $wpdb is the way to go. Just remember to use prepare, if there's any user submitted stuff included in the query.
If on the other the data is stored as posts (with custom post type) with associated post meta, then WP_Query with meta_query argument is more appropriate way to find the data. (It uses $wpdb in the end, but is the more typical way to query posts).