Displaying multiple loops based off of category

You can use wp_get_post_terms() and in_array() to check if “investments” is in the term array or not: <?php $catquery = new WP_Query(array( ‘orderby’ => ‘date’, ‘order’ => ‘DESC’ )); ?> <?php while($catquery->have_posts()) : $catquery->the_post(); ?> <?php $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), ‘large’ );?> <?php // Gather all the post terms for the post. $post_term_objects = wp_get_post_terms( … Read more

How to vary post loop results layout and resume?

How do I get a WP_Query object containing the next five posts? : // $query is the original WP_Query object. $five_posts = new WP_Query(); $five_posts->posts = array_slice( $query->posts, $query->current_position, $query->current_position + 5 ); How do I then elegantly resume the standard-layout loop output at the right place? : /* Sum the count of all the … Read more

How to get an array of years from all of the search results, and use it to filter by year the paginated loop?

This is really interesting one! I’ve tried to solve this, and here is what I ended up with. With a pure SQL, we have an ability to get all distinct years of all our posts with the following SQL request (recipe founded here): SELECT DISTINCT YEAR(wp_posts.post_date) FROM wp_posts getting something like this: mysql> SELECT DISTINCT … Read more

How to order posts by meta_value created inside loop?

In your WP_Query, only return ids. https://developer.wordpress.org/reference/classes/wp_query/#return-fields-parameter Create an empty array, $myArray = array();. Then loop thru the results of the query, $your_query->posts and fetch your field value for each id and add the result and the id to a new array in $myArray. Then sort the arrays in $myArray by the price. Then do … Read more

WordPress Loop – Not returning projects in specific category

You’re sure that you have actual posts (project post type), that have that category selected on them, right? Make sure the posts have an actual category selected, not a tag. Also check in the categories configuration to make sure that websites is the actual slug of the category they are assigned to. You can also … Read more

Use object in template part

As of WordPress 5.5 you can pass variables to template parts by passing them in an array to the third argument of get_template_part(): foreach ($categories as $category) { get_template_part( ‘temp-parts/loop/blcnr_loop’, null, [ ‘category’ => $category ] ); } These variables will populate an $args variable accessible from the the template: echo $args[‘category’]->name;

How to track post number on the_loop

This should do it: $real_current_post = $wp_query->current_post + 1; switch( $real_current_post ){ case 2: // Do something for 2 break; case 5: // Do something for 5 break; case 7: // Do something for 7 break; } You may need to globalize $wp_query, but as long as you’re using the normal template hierarchy, it should … Read more