Custom Post Type Loop throws 500 error when used in widget

There are several problems in your provided code. $query -> while( have_posts() ) The WP_Query() return type is object. You are referring to a method that doesn’t exist. Instead, you should use the following: while( $query->have_posts() ) {…} wp_reset_postdata(); inside the conditional This function resets the post’s data, as it suggests. If you use it … Read more

Display post from current category and same tag?

Tags are a taxonomy called post_tag. You can use them in get_posts() via the tax_query. Since wp_get_post_tags() returns an array of objects, you need to clean it up a bit since only one field per object is required for the query. $tag_objects = wp_get_post_tags($post->ID); $tags = array(); foreach ($tag_objects as $tag_object) { $tags[] = $tag_object->term_id; … Read more

A loop with all articles with certain text strings in tags

Based on @birgire’s always-useful comments, I did write a workaround for you. What I’m doing is to first find the terms that match the search string. Then, based on the found terms I will do a query in those. function wpse_posts_by_tagname( $string ) { // Let’s find every tag that has ‘coffee’ in it’s name … Read more

Loop first six posts in carousel, next eight in grid

Use WP_Query with offset like this <?php $offset = 6; $post_args = array( ‘post_type’ => ‘post’, ‘posts_per_page’ => $offset, ); $slider_blog_posts = new WP_Query( $post_args ); ?> <?php if ( $slider_blog_posts->have_posts() ): ?> <div class=”carousel”> <?php while( $slider_blog_posts->have_posts() ): $slider_blog_posts->the_post(); ?> <?php get_carousel_slide( get_the_ID, true); ?> <?php endwhile; ?> </div> <?php endif; ?> <?php wp_reset_postdata(); … Read more

Additional featured post on first page

If I understand your goal correctly, I would create two queries. The first will retrieve only the featured post. We’ll display that post on the first page only and store its ID to use later. Then, a second query will get all of the remaining posts. // first query to retrieve the featured post $featured_post_id … Read more

Avoid duplicate post from same Taxonomy

I see two ways to go about it, both quite similar. A) You can collect IDs of displayed posts in an array and then exclude those with post__not_in query parameter like so: $years_loop = get_terms( array( ‘taxonomy’ => ‘works_year’, ‘orderby’ => ‘slug’, ‘order’ => ‘DESC’, ) ); // array for displayed posts, we will update … Read more