Replace query_posts with pre_get_posts

Without seeing the broken code it is hard to say, but your filter should look something like this: function pregp_wpse_97354($qry) { if (is_front_page() && is_main_query() && is_user_logged_in()) { $qry->set(‘cat’,2); $qry->set(‘posts_per_page’,5); $qry->set(‘orderby’,’date’); $qry->set(‘order’,’DESC’); } } add_action(‘pre_get_posts’,’pregp_wpse_97354′); You mentioned something about the user being logged in, which your code does not check for, but I threw that … Read more

is_category in pre_get_posts results in php notices

is_category(‘category-name’) won’t work until after the query is run. If we check source code where those notices are generated, we can see that it’s using get_queried_object, which gets populated with the results of the main query, and at the pre_get_posts stage will still be empty. As an alternate, check the contents of $query->query_vars, which will … Read more

Archive by custom post type and custom date field

Thanks to Milo for helping me through this. Here is the code that worked for me. function custom_events_query( $query ) { if ( $query->is_main_query() && $query->is_archive && !is_admin()&& is_post_type_archive(‘events’) && $query->query_vars[‘m’]!=”) { $query->set( ‘post_type’, ‘events’ ); $query->set( ‘meta_key’, ‘event_date’ ); $query->set( ‘orderby’, ‘meta_value’ ); $query->set( ‘order’, ‘DESC’ ); $query->set( ‘post_per_page’, 10 ); unset($query->query_vars[‘year’]); unset($query->query_vars[‘monthnum’]); unset($query->query_vars[‘day’]); … Read more

pre_get_posts and the blog page

Your blog page is actually your home page, so you need to use is_home(). For the static frontpage, is_front_page() would be used EDIT Sorry for being confusing :-). This how it works when a static frontpage is set The page set as your frontpage is actaully just a cover page. When you think of a … Read more

Date Query to Pull Current and Future Posts

I wonder if you mean this kind of date_query: $query->set( ‘date_query’, [ [ ‘after’ => ‘today midnight’, ‘column’ => ‘post_date_gmt’, ‘inclusive’ => true, ], ] ); where we use the after attribute. Here the after date will be calculated by WP_Date_Query as: gmdate( ‘Y-m-d H:i:s’, strtotime( ‘today midnight’, current_time( ‘timestamp’ ) ) ); With the … Read more

Include custom post type in “all posts”

Filter pre_get_posts: add_filter( ‘pre_get_posts’, ‘wpse_98213_add_post_types_to_tax_query’ ); /** * Let WP search for custom post types on taxonomy archives. * * @wp-hook pre_get_posts * @param object $query * @return object */ function wpse_98213_add_post_types_to_tax_query( $query ) { if ( ! is_main_query() or ! is_tax( ‘your_taxonomy_name’ ) ) return $query; $query->set( ‘post_type’, array ( ‘portfolio’, ‘post’ ) ); … Read more

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