Changing the meta_query of the main query based on custom query_vars and using pre_get_posts

Instead of trying to display all the matching events on your by-date page, you could try to display through ?post_type=event like this: function rewrite_rule_by_date() { add_rewrite_rule(‘by\-date/([0-9]{4}\-[0-9]{2}\-[0-9]{2})$’, ‘index.php?post_type=event&event_date=$matches[1]’, ‘top’); } add_action( ‘init’, ‘rewrite_rule_by_date’ ); function query_var_by_date() { add_rewrite_tag( ‘%event_date%’, ‘([0-9]{4}-[0-9]{2}-[0-9]{2})’); } add_action( ‘init’, ‘query_var_by_date’ ); function custom_event_query( $query ) { if ( get_query_var( ‘event_date’ ) && … Read more

is_category() in pre_get_posts strange error

After a bit of investigation… If you pass a category to is_category it uses get_queried_object to grab data– see the source. get_queried_object returns NULL for categories that do not exist. You can demonstrate that with: function custom_posts_per_page($query) { var_dump(get_queried_object()); } add_filter( ‘pre_get_posts’, ‘custom_posts_per_page’ ); Load a valid category archive and then an invalid one. The … Read more

WP_Query: query posts by ids from array?

You have to use post__in (double underscore) argument, instead of post_in: echo print_r($rel); // Array ( [0] => 63 [1] => 87 ) $args = array( ‘post_type’ => array( ‘post’ ), ‘orderby’ => ‘ASC’, ‘post__in’ => $rel ); $loop = new WP_Query( $args ); If you are unsure why an argument is not working, copy … Read more

Random post, once per day

First of all you really shouldn’t be using query_posts(). Read this excellent explanation why. Then this is a perfect use case for transients. You just get the post once and then cache it for 24 hours using the Transients API. if ( false === ( $quotes = get_transient( ‘random_quote’ ) ) ) { // It … Read more

how to get a different html for odd/even posts?

You don’t need a new variable for counting posts, WordPress has one already in $wp_query->current_post. <?php while (have_posts()): the_post() ?> <?php if ($wp_query->current_post % 2 == 0): ?> even <?php else: ?> odd <?php endif ?> <?php endwhile ?> If you use a custom WP_Query instance as iEmanuele suggested then it will be $query->current_post instead.

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