Nested meta_query with multiple relation keys

The question was for WordPress 3.0, but just in case someone has the same question for a more recent version, from WordPress Codex: “Starting with version 4.1, meta_query clauses can be nested in order to construct complex queries.” https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters So, that query should work on the current WordPress version.

Custom query with orderby meta_value of custom field

You can define the meta key for orderby parameter using the old method (I tested on WP 3.1.1)… query_posts( array( ‘post_type’ => ‘services’, ‘order’ => ‘ASC’, ‘meta_key’ => ‘some_key’, ‘orderby’ => ‘meta_value’, //or ‘meta_value_num’ ‘meta_query’ => array( array(‘key’ => ‘order_in_archive’, ‘value’ => ‘some_value’ ) ) ) );

How do I query a custom post type with a custom taxonomy?

Firs of all don’t use query_posts() ever, read more about it here: When should you use WP_Query vs query_posts() vs get_posts()?. You have to use WP_Query to fetch posts what you need. Read documentation for it. In your case the query could be like this: $the_query = new WP_Query( array( ‘post_type’ => ‘Adverts’, ‘tax_query’ => … Read more

posts_per_page no limit

-1 is your answer! Look for posts_per_page here. $args = array( ‘post_type’ => ‘post’, ‘cat’ => ‘22,47,67’, ‘orderby’ => ‘name’, ‘order’ => ‘ASC’, ‘hide_empty’ => 1, ‘depth’ => 1, ‘posts_per_page’ => -1 ); Important caveat: This can result in a very huge query that can bring the site down. Do this only if you are … Read more

When to use WP_query(), query_posts() and pre_get_posts

You are right to say: Never use query_posts anymore pre_get_posts pre_get_posts is a filter, for altering any query. It is most often used to alter only the ‘main query’: add_action(‘pre_get_posts’,’wpse50761_alter_query’); function wpse50761_alter_query($query){ if( $query->is_main_query() ){ //Do something to main query } } (I would also check that is_admin() returns false – though this may be … Read more

When should you use WP_Query vs query_posts() vs get_posts()?

query_posts() is overly simplistic and a problematic way to modify the main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination). Any modern WP code should use more reliable methods, like … Read more