$query->query_var[‘post_type’] not set for pages

To clear up some confusion in previous answers, pre_get_posts isn’t a filter so you don’t need to return anything. Your only problem that I see is the if: if ( !is_search() && !in_array( get_post_type(), $post_types ) ) return; Basically get_post_type() is going to return false during pre_get_posts because the global $post hasn’t been set yet … Read more

Exclude category on blog list page

From a plugin or your theme’s functions.php file: function wpse106861_mod_query( $query ) { /* are we on the blog page ? */ if ( $query->is_home() && $query->is_main_query() ) { /* get ID of news category */ $news_id = get_cat_ID( ‘news’ ); /* exclude posts in new from query */ $query->set( ‘category__not_in’ => array( $news_id ) … Read more

Show only posts which can be commented

Because comment_status is not a built-in query argument – but you can easily implement it: function wpse_214323_query_by_comment_status( $where, $wp_query ) { global $wpdb; if ( ! empty( $wp_query->query_vars[‘comment_status’] ) ) { $status = $wp_query->query_vars[‘comment_status’]; if ( $status !== ‘open’ ) $status=”closed”; $where .= ” AND $wpdb->posts.comment_status=”$status””; } return $where; } add_filter( ‘posts_where’, ‘wpse_214323_query_by_comment_status’, 10, 2 … Read more

How to pass custom parameter to WP_Query for filtering in pre_get_posts

You can access query variables (including custom ones) via the WP_Query::get() method. For example: $my_query = new WP_Query( array( … ‘wpse105219_custom_var’ => ‘foobar’, … ) ); To ‘catch’ this at pre_get_posts: add_action( ‘pre_get_posts’, ‘wpse105219_pre_get_posts’, 10 ); function wpse105219_pre_get_posts( $query ){ if( $query->get( ‘wpse105219_custom_var’ ) == ‘foobar’ ){ //Alter $query } }

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