How Can I Change The Tax Query For The Main Loop For Taxonomy Archives?

@birgire answer is fine (+1 from me), however $query->tax_query->queries can contain more than one taxonomy, and $query->tax_query->queries[0] can be the query for another taxonomy. So, if you want to set ‘include_children’ to false (note that it is a boolean argument) on the taxonomy ‘TAXONOMY NAME’, then you should be sure that you act on right … Read more

pre_get_posts and set

pre_get_posts triggers for every query (front and admin side). It also triggers when querying nav-menu items (which is what is happening here). Most of the time you only want to filter particular queries – so you need to check if that query is one you wish to modify. Usually – and I think your example … Read more

Shouldn’t I be able to modify the main query by this filter?

The reason your code isn’t working is because by the time archive.php runs, the pre_get_posts hook has already fired. You can easily accomplish the task in your functions.php file: add_action( ‘pre_get_posts’, ‘wpse_pre_get_posts’ ); function wpse_pre_get_posts( $query ) { remove_action( ‘pre_get_posts’, ‘wpse_pre_get_posts’ ); //* $query->have_posts() will always return false from this hook //* So get the … Read more

how to restrict posts_request filter to the main query only

The posts_request filter actually takes a second argument, which is the query. You can check if that query is the main query. Try this: add_filter( ‘posts_request’, ‘my_request_filter’, 10, 2 ); function my_request_filter($sql, $query) { if($query->is_main_query() && is_search()) { $sql=”some custom sql query” } return $sql; }

Include Site 1 Posts in Query for Sub Sites

If you want to work with it just like with WP_Query, you can use this construction. $query = new Network_Query( array( ‘blog_id’ => array( 1, get_current_blog_id() ), ‘posts_per_page’ => 5, ‘date_query’ => array( ‘after’ => date(‘Y-m-d’, strtotime(‘-1130 days’)) ), ‘meta_query’ => array( array( ‘key’ => ‘shows’, ‘value’ => $id, ‘compare’ => ‘LIKE’, ) ) ) … Read more

Modify the main loop to display current month / year

have you tried adding a second $query->set() ? function loop_current_month( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( ‘monthnum’, date(‘m’) ); $query->set( ‘year’, date(“Y”) ); } } add_action( ‘pre_get_posts’, ‘loop_current_month’ ); This should get all the posts from the current month or this year, note date(‘m’) and date(‘Y’)

I am trying to hide a custom post type category to logged in users with Pre_Get_Posts

OK, so you have few mistakes in there. field can be term_id or slug, but not id. tax_query should be array of arrays and not an array. So here is fixed code: function exclude_category( $query ) { if ( is_user_logged_in() && $query->is_main_query() ) { $taxquery = array( array( ‘taxonomy’ => ‘project-type’, ‘field’ => ‘term_id’, ‘terms’ … Read more

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