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

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

Inserting custom data to the_post() during loop

Yep, it’s not very common, but perfectly viable and nifty technique. After the posts are retrieved in WP_Query they are passed through the_posts filter like this (where $this is WP_Query instance): $this->posts = apply_filters_ref_array( ‘the_posts’, array( $this->posts, &$this ) ); You just loop through array and assign the extra data you need to objects. Don’t … Read more

Loop returning only 1 result

Near the top of your code, you have wp_reset_postdata();. As explained in the Functional Reference it alter the main loop and should be avoided. Looks like that’s what’s causing your issue.

Any number in meta key (wp query)

You must use LIKE on the meta_key to support the SQL % pattern. The compare attribute only applies to the meta value. Instead you could try to filter it with: /** * Match any numbers in given a meta-key for WP_Query * * @see https://wordpress.stackexchange.com/a/177331/26350 */ ! is_admin() && add_filter( ‘posts_where’, function( $where ) { … Read more