How to know which one is the main query?

WP_Query->is_main_query() method source (which function of same name calls) is very short and simple: function is_main_query() { global $wp_the_query; return $wp_the_query === $this; } The main query is the query that is stored in $wp_the_query global. But what is that global? When WP sets up main query it stores it in two places: $wp_the_query and … Read more

Alter query on edit.php

Few days ago I wrote a quick solution using pre_get_posts filter to hide some pages in admin area. Maybe you could use it as a good starting point for whatever you’d like to achieve. if ( is_admin() ) add_filter(‘pre_get_posts’, ‘mau_filter_admin_pages’); function mau_filter_admin_pages($query) { $query->set(‘post__not_in’, array(1,2,3) ); // Do not display posts with IDs 1, 2 … Read more

post_name empty after wp_insert_post

Firstly, you shouldn’t use post_category, because according to the wordpress codex wp_insert_post(): ‘post_category’ => [ array(, <…>) ] //post_category no longer exists, try wp_set_post_terms() for setting a post’s categories Secondly, if you want the post title to be your slug you shouldn’t need to use the post_name parameter, because it gets constructed from the title … Read more

Are there any scenarios where the query_posts may be used?

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.

How to order posts by descending comment count on taxonomy page?

A friend helped me with a solution that I could place in my functions.php file: add_filter( ‘pre_get_posts’, ‘my_pre_get_posts’ ); function my_pre_get_posts( $query ) { if ( is_tax( ‘locations’ ) && empty( $query->query_vars[‘suppress_filters’] ) ) $query->set( ‘orderby’, ‘comment_count’ ); return $query; }

Does the ‘cat’ argument in query_posts fetch posts from subcategories as well as the given ID?

In short: Yes. Your question is about the query_posts WordPress PHP function. The link I’ve placed is the official wordpress codex documentation for that function. For your example it states: Display posts from only one category ID (and any children of that category): query_posts(‘cat=4’); I hope this information is helpful. You find more infos in … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)