wp_query conditionally get posts

You can check if a query has posts with if( $your_query->have_posts() ), and only output the container if it passes that test. $featuredPosts = new WP_Query( array( ‘posts_per_page’ => 2, ‘cat’ => 6, ‘offset’ => 4 ) ); if ( $featuredPosts->have_posts() ) : echo(‘<ul class=”right-hang”>’); while ( $featuredPosts->have_posts() ) : $featuredPosts->the_post(); endwhile; echo ‘</ul>’; endif;

Pagination With Custom WP Query not functioning

One issue at least: you’ll want to close the loop before outputting your pagination links, or else they’ll be output on every iteration of the loop. But you’ve got paginate_links() before endwhile;. Try this arrangement instead: // open loop if ( have_posts() ) : while ( have_posts() ) : the_post(); // Loop markup // close … Read more

Including ‘cat’ => $cat breaks meta_query

Not sure why this works, where the other query was failing. I’m not sure if it’s a bug that others are experiencing or not. Anytime I included any category “inclusive” arg, it broke my meta_query, but it didn’t seem to if i was “excluding” categories. So,…making an array of all category ids, then unsetting the … Read more

Make assigning post to a specific category equivalent to assigning it to all categories

One way to deal with the problem is to use the redirect_canonical filter to cancel the redirect (that happens when the category that the post is assigned to doesn’t match the category slug in the requested post URL) for posts assigned to International (intl) category. /* * Related core file: wp-includes/canonical.php * * NOTE: If … Read more

Printing direct descendants of a category with WP_Query

From the Codex: child_of (integer) Display all categories that are descendants (i.e. children & grandchildren) of the category identified by its ID. There is no default for this parameter. If the parameter is used, the hide_empty parameter is set to false. And… parent (integer) Display only categories that are direct descendants (i.e. children only) of … Read more

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