Ordering a mysql style datetime stamp with meta_query

‘orderby’ => ‘meta_value_num’ will try to force the value to a number by adding 0. In other words, “2013-09-30 22:00:00″+0 When I tested that both PHP and MySQL converted the string to 2013, which is not what you want. That particular date format, and only that date format, will sort correctly if you just leave … Read more

Set colors depending on category

Setting aside that you’re incorrectly using query_posts(), the easiest solution is to add a call to post_class(), to output post-specific classes, including .category-{ID} and .category-{slug}: <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?> <li <?php post_class(); ?>><a href=”https://wordpress.stackexchange.com/questions/123066/<?php the_permalink(); ?>” title=”<?php the_title(); ?>”><strong><?php the_title(); ?></strong></a><br /> <em><?php echo substr(get_the_excerpt(), 0,110); ?> … Read more

WP Query with multiple tags by get the tags

If you read the Codex entry, you must pass an array as tag_slug__in. In this case though, I would use tag__in with IDs rather than slugs: $tag_ids = array(); foreach( get_the_tags($post->ID) as $tag ) { $tag_ids[] = $tag->term_id; } $recent_posts = new WP_Query( array( ‘tag__in’ => $tag_ids, ‘posts_per_page’ => 2, ‘cat’ => 20 ) );

Query Vars Not Set in Main Query

Query vars are… fun like that. Depending on which archive you are messing with get_post_type() just might be simple enough to do it (it gets type from actual post, not query). Otherwise you might want to go for get_queried_object() to figure out what precisely you are dealing with.

Using Wp_Query without the loop?

The query itself doesn’t change globals, however you are correct that the_post() does. The most contained way to just retrieve posts (as array of results) is using get_posts() function, which is wrapper around WP_Query and takes same arguments.

Problem with WP_query

Your second block of code is strange. The use of query_posts() doesn’t really make sense. I don’t even know what that is intended to do. Additionally, that is not really the best way to exclude posts from a Loop, and is what is causing your 5 vs. 6 posts issue. You need to exclude the … Read more

Nested WP_Query breaking loop

This can be done in a single query: $parent_only = new WP_Query( array( ‘post_parent’ => 0, ) ); If you want to have all children of a specific page (for e.g.) in the loop (or on a request that returns TRUE for is_singular()), use get_the_ID() as argument for post_parent. Edit (after seeing the full loop … Read more

How to cache wp_query with pagination using transients?

You need to set a unique transient key for each page of posts otherwise each page will output same posts. Here is a example. Focus on get_transient & set_transient functions. <?php $my_posts_query = get_transient( ‘my_unique_transient_key_’ . get_query_var( ‘paged’ ) ); if( false === $my_posts_query ) { $args = array( ‘post_type’ => ‘post’, ‘posts_per_page’ => ’12’, … Read more