How to get all unique categories for posts in loop?

EDIT: Moved $i = 0 outside the loop. That should get you the full list of categories. $get_posts = new WP_Query(); $i = 0; $get_posts->query($args); if($get_posts->have_posts()) { $cats = array(); while($get_posts->have_posts()) { $get_posts->the_post(); $post_categories = wp_get_post_categories( get_the_ID() ); foreach($post_categories as $c){ $cat = get_category( $c ); $cats[$i] = $cat->slug ; $i++; } } //endwhile $result … Read more

WP_Query posts_per_page Only Bringing Back one post

Looks like you might be missing the wp_reset_postdata() in here. The WordPress codex shows you need to have a wp_reset_postdata() after your while loop. Here’s WordPress’ example (which even has the posts_per_page defined): <?php // example args $args = array( ‘posts_per_page’ => 3 ); // the query $the_query = new WP_Query( $args ); ?> <?php … Read more

Future post ID not showing

The problem is that you are using get_the_ID() outside the loop. Also, you are getting the global $post overriding the $post object passed to the function and then overriding it again with get_post() function. As you have already the $post object which status has changed, you need nothing of the get_the_ID, get_post or global $post. … Read more

How to use an associative array in post__in with WP_Query?

Take a look at PHP’s array_values function. Alternatively, you could typecast. Note that if the array is in serialized form as you have put it above, you will have to unserialize it first, in either case. $numerical_array = array_values( unserialize( ‘a:1:{s:8:”post-134″;s:3:”134″;s:8:”post-136″;s:3:”136″;}’ ) ); or $numerical_array = (array) unserialize( ‘a:1:{s:8:”post-134″;s:3:”134″;s:8:”post-136″;s:3:”136″;}’ );

Enter a variable in the ‘category_name’ parameter

Yes you can pass the variable in category_name. But I think it should be get_the_title() instead of the_title(). $title = get_the_title(); $args = array( ‘category_name’ => $title ); Try this. But remember category_name only accepts category slug, not category name. You can also use following method if your page title/slug are different. And I think … Read more