Query posts by custom fields

I managed to figure it out (finally) after about 12 hours of screwing around. The answer here gave me 98% of the answer, and the rest was simply using the appropriate hooks to modify it for my use and to include the postmeta table.

wp query problem

While comparing numbers in meta value use “meta_value_num” instead of “meta_value” ‘meta_value_num’ – Order by numeric meta value (available with Version 2.8). Also note that a ‘meta_key=keyname’ must also be present in the query. This value allows for numerical sorting as noted above in ‘meta_value’. Reference

$query conflicting with other queries in the same page

function pre_get_posts( $query ){ if(isset($query->getpostlist) && $query->getpostlist) { add_filter( ‘posts_fields’, ‘postsetupFields’, 20); } else { remove_filter( ‘posts_fields’, ‘postsetupFields’, 20); } return $query; } function postsetupFields( $fields ){ global $wpdb; $fields = “{$wpdb->posts}.ID”; return $fields; } add_action( ‘pre_get_posts’, ‘postsetupFields’);

Multiple loop for “featured” items returns wrong posts

Your second loop is fine because it works – although it is probably more efficient to do it this way: $args = array( ‘post_type’ => ‘onourradar’, ‘posts_per_page’ => -1, ‘orderby’ => ‘menu_order’, ‘order’ => ‘ASC’, ‘offset’ => ‘2’); $my_query = new WP_Query($args); while ($my_query->have_posts()) : $my_query->the_post(); The difference is that a do_not_duplicate query is just … Read more

Is it possible to use WP Query to orderby custom meta value and custom taxonomy?

I have the similar query in my blog. This my args array : $args= array( ‘post_type’ => ‘box’, ‘posts_per_page’ => -1, ‘meta_key’ => ‘position’, ‘order’ => ‘DESC’, ‘orderby’ => ‘meta_value_num’, ‘tax_query’ => array( array( ‘taxonomy’ => ‘taxonomyname’, ‘field’ => ‘slug’, ‘terms’ => ‘yourterm’ ) ) ); Hope it will help you

Using Query In Post Type Archives

Change it from: ‘category_name’ => celebrity, ‘orderby’ => 1, into ‘category_name’ => ‘celebrity’, ‘orderby’ => ‘date’, // or something else Otherwise celebrity will be treated as a constant name, not a string. Also, you can’t order by 1, you have to use one of the ordering parameters.