Hot answers tagged

6

You ask: is query_posts really slower than some secondary query... The fact is that if you're calling query_posts() from a theme then it already is a secondary query. WordPress has already queried the database once to get a certain page then it hits your query_posts() function and queries the database again creating a second query and overwriting the ...


5

There is no need to do an overrated amount of queries to accomplish this. you can still use only one query, the_posts filter and some PHP to sort your code as needed. I take that this is a custom query from what I read in your question, so we can do the following: First, we want to introduce a custom WP_Query parameter so that we can use that parameter ...


4

Skimming through the WP_Query we find this part of interest: if ( !$q['suppress_filters'] ) { /** * Filter the completed SQL query before sending. * * @since 2.0.0 * * @param array $request The complete SQL query. * @param WP_Query &$this The WP_Query instance (passed by reference). */ $this->request ...


4

query_posts() is useful in cases when there is no main query: calls to wp-admin/admin-ajax.php, wp-admin/admin-post.php or wp-login.php for example. Yes, you can achieve the same results there without query_posts() and slightly less compact code instead. But if you don't have to care about side effects, using query_posts() is acceptable.


3

In general, the query performed on the homepage, query_posts( 'posts_per_page=get_option( 'posts_per_page' ) and $q = new WP_Query( 'posts_per_page=get_option( 'posts_per_page' ) should have the same exact performance with very very little to no difference between them as all of the above are exactly the same by default (that is, have the same query ...


3

Whenever the main query is available, which is for every front end page load regardless of which page/archive is loaded, you should use pre_get_posts to alter the main query's query vars before the SQL query is build and executed. This goes for each and every page where you need to alter the main query. This is the RECOMMENDED way to alter the main query. ...


2

For reference, before: 45q, after: 42q The code is very similar to the code used by @birgire function _tomjn_home_cancel_query( $query, \WP_Query $q ) { if ( !$q->is_admin() && !$q->is_feed() && $q->is_home() && $q->is_main_query() ) { $query = false; $q->set( 'fields', 'ids' ); } return ...


2

Here is a neat trick I learned from @birgire, we can halt the main query by appending AND where 0=1 to the WHERE clause of the SQL query. This might still result in one db query, but it will surely stop the main query from querying posts add_filter( 'posts_where', function ( $where, \WP_Query $q ) { if ( $q->is_home() && ...


1

If we use: $args = array( 'post_type' => 'custom_post_type', 'post_status' => 'publish', 'posts_per_page' => 9, 'meta_key' => 'pkg_id', 'orderby' => array( 'pkg_id' => 'ASC', 'rand' => 'DESC' ) ); then we get the order part as: ORDER BY wp_postmeta.meta_value ASC, RAND() DESC This seems to ...


1

Not possible with a single query you would have to do 3 seperate query each one targeting the different "Package ID's" and ordering those queries as rand. $args1 = array( 'post_type' => 'custom_post_type', 'orderby' => 'rand', 'meta_query' => array( array( 'key' => 'pkg_id', 'value' => 1, // you ...



Only top voted, non community-wiki answers of a minimum length are eligible