How to get post meta value and post in one query?

When you do a query WordPress already gets the post_meta. Even though you are calling get_post_meta() It is not performing another query. It is retrieving the value from the post meta cache. Now if you don’t need the post meta you can set a flag that will eliminate the additional MySql query. To do this … Read more

tax_query in WP_Query problem

Thanks to Rarst for pointing me in the right direction. After a bit of playing around the following query is now producing the effect I’m after: $query_args = array( ‘numberposts’ => $limit, ‘order’ => ‘DESC’, ‘orderby’ => ‘post_date’, ‘post_type’ => ‘match’, ); $query_args[‘meta_query’] = array( ‘key’ => ‘played’, ‘value’ => true, ); if ( isset( … Read more

Query Posts but only return 1 from a custom taxonomy?

I think that using standard WP_Query you should run 6 different loops, but this is overwhelming. But using a custom $wpdb query you can do the trick… function get_unique_posts_tax( $taxonomy = ‘category’, $number = 10, $cpt=”post”) { global $wpdb; return $wpdb->get_results( $wpdb->prepare( “SELECT p.* FROM $wpdb->term_relationships JOIN $wpdb->term_taxonomy AS tt ON tt.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id JOIN … Read more

Shortcode returns escaped HTML tags

I would recommend using single quotes if you want to include some HTML, also you need to return something otherwise nothing happens. // WP Shortcode function text_shortcode() { return ‘<strong>bold text:</strong> <a href=”https://wordpress.stackexchange.com/questions/318934/shortcode-returns-escaped-html-tags”>See wordpress.stackexchange.com</a>’; } add_shortcode(‘bold-text’, ‘text_shortcode’); Your WordPress shortcode would be: [bold-text] Edit: You don’t need html_entity_decode or htmlentities only when you’re doing something … Read more

How to order a post type with meta_value_num and if meta_value_num does not exist then order by date

You can pass a space-delimited set of columns to orderby argument of WP Query: $args = array( ‘meta_key’ => ‘wp_ratings’, ‘orderby’ => ‘meta_value_num date’ ); $query = new WP_Query( $args ); You can also pass an array of key => sort_order for more granular control, for example: $args = array( ‘meta_key’ => ‘wp_ratings’, ‘orderby’ => … Read more

Best practice for multiple queries on page

WP_Query can handle all of these cases. See the section on handling multiple taxonomies. $this_cat=”interviews”; $this_tag = ‘angelina-jolie’; $args = array( ‘tax_query’ => array( ‘relation’ => ‘AND’, array( ‘taxonomy’ => ‘category’, ‘field’ => ‘slug’, ‘terms’ => $this_cat ), array( ‘taxonomy’ => ‘post_tag’, ‘field’ => ‘slug’, ‘terms’ => $this_tag ) ) ); $interviews = new WP_Query( … Read more