What’s Your Query? WCCHI 2013
The What, Where, Why, and Why Not
-Jarrod Pyper
This is not about jQuery. Sorry.
This is not about jQuery. Sorry.
There are 3 ways to request posts from WP:
“query_posts() is wasteful and ungrateful”.
-query_posts() own mother.
At best, you’re doubling the amount of database queries.
At worst, you’re breaking other parts of the page and other plugins.
At least use wp_reset_query() when you’re done.
Always Prepared – Runs 4 queries for you
Easy to get along with
The default arguments setup is our fastest choice of the 3
Typical WP_Query format is:
$args = array(
'post_type' => 'my_post_type',
'posts_per_page' => -1,
'orderby' => 'rand'
);
$my_query = new WP_Query($args);
while( $my_query->have_posts() ) : $my_query->the_post();
//Do stuff
endwhile;
wp_reset_postdata();
If you’re still going to use query_posts or just realized why something was breaking on a site and need to fix it before lunch:
wp_reset_query();
If you need to alter the main query (as query_posts() does) here’s a better way:
function my_other_query( $query ) {
if( $query->is_main_query() ) {
//Yeah it's the main one - do something to alter it
$query->set('post_type', 'my_post_type');
}
}
add_filter('pre_get_posts', 'my_other_query');
$args = array( 'post_type' => 'product', 'meta_query' => array( array( 'key' => 'color', 'value' => 'blue', 'compare' => 'NOT LIKE' ) ) ); $query = new WP_Query( $args );
$args = array( 'post_type' => 'product', 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'color', 'value' => 'blue', 'compare' => 'NOT LIKE' ), array( 'key' => 'price', 'value' => array( 20, 100 ), 'type' => 'numeric', 'compare' => 'BETWEEN' ) ) ); $query = new WP_Query( $args );
$args = array( 'post_type' => 'post', 'tax_query' => array( 'relation' => 'OR', array( 'taxonomy' => 'category', 'field' => 'slug', 'terms' => array( 'quotes' ) ), array( 'taxonomy' => 'post_format', 'field' => 'slug', 'terms' => array( 'post-format-quote' ) ) ) ); $query = new WP_Query( $args );
print_r is nice but is not easy to read, this function lets you specify the variable to print, a short message to print before the variable output, and wether you are echoing or returning the entire thing (useful for using the error_log).
function printr($out, $msg='out', $return=false) {
if($return == false)
echo '<pre style="overflow: scroll;">'.$msg.': '.print_r($out,1).' |</pre>';
else
return '<pre style="overflow: scroll;">'.$msg.': '.print_r($out,1).' |</pre>';
}
Add this to your wp_config.php file. This will allow you to get all of your php errors and warnings output to the page you are on by putting “?errors=on” at the end of your URL. This can come in handy for debugging or analyzing a single page on a live site.
if ( isset($_GET['errors']) && $_GET['errors'] == 'on'){
@ini_set('display_errors','On');
define('WP_DEBUG', true);
} else {
define('WP_DEBUG', false);
}
function v_database_output($atts) {
global $wpdb;
extract( shortcode_atts( array(
'blogid' => '', //Not entering a blogid will result in current blog being used
'table' => $wpdb->options, //Default to current blog's options table
'columns' => '*', //Default to get all columns from table
'prefix' => 'yes'
), $atts ));
if($table != $wpdb->options) {
if($blogid != '')
$table = $wpdb->base_prefix.$blogid.'_'.$table;
else
$table = $wpdb->prefix.$table;
}
if( $prefix == 'no' )
$table = $table;
$query = 'SELECT '.$columns.' FROM '.$table;
$results = $wpdb->get_results("$query");
$out = 'Your query: '.$query.'<br/><br/><pre style="overflow:scroll;">'.print_r($results,1).'</pre>';
return $out;
}
add_shortcode('dbquery', 'v_database_output');