Hide elements outside loop based on query

You can use the function wp_list_pluck to get the ID of the posts retrieved from the query. Just move the query before the h2 element and get the posts ID’s: // Set a bool to know if at least one post has post thumbnail. $has_thumb = false; $first_query = new WP_Query(‘cat=1&author=” . $post->post_author . “&order=DESC&tag=Tattoo&posts_per_page=1000’); … Read more

WP_Query returns posts_per_page + 1 every time

I’ve seen this happen when you have sticky posts. Try excluding them, like this: <?php $query = new WP_Query([ ‘post_status’ => ‘publish’, ‘orderby’ => ‘date’, ‘order’ => ‘DESC’, ‘posts_per_page’ => 4, ‘ignore_sticky_posts’ => true, ]); ?>

Possible to alternate between two loops

You need two queries for this. Let’s say the ID of the featured category is 5. Then you set up two queries retrieving 3 posts each: $query1 = new WP_Query( array( ‘cat’ => ‘5’, ‘posts_per_page’ => 3 ) ); $query2 = new WP_Query( array( ‘cat’ => ‘-5’, ‘posts_per_page’ => 3 ) ); Now you have … Read more

Get author if post has multiple authors

The Co-Authors Plus plugin uses it’s own template tags which replace the default WordPress tags, the_author() and the_author_posts_link(). coauthors() – Outputs the co-authors display names, without links to their posts. coauthors_posts_links() – Outputs the co-authors display names, with links to their posts. coauthors_links() – Outputs the co-authors display names, with links to their websites if … Read more

Print current post category during WP_Query

You are using the get_the_terms() but this function is used to get the custom taxonomies. Use the function get_the_category(); inside the WordPress loop and pass the get_the_ID() function which will get the current post ID in the loop. get_the_category(get_the_ID()); <?php $args = array( ‘post_type’ => ‘work’, ‘posts_per_page’ => ‘9’ ); $work_loop = new WP_Query( $args … Read more

Order by most used tag

I’m afraid orderby taxonomy terms post count is not possible by WP_Query. See this answer. So here I’ve written a SQL based function for you. It’ll order your posts based on terms post count. function the_dramatist_order_posts_by_tag_count( $args=”” ) { global $wpdb; $param = empty($args) ? ‘post_tag’ : $args; $sql = $wpdb->prepare(“SELECT posts.*, terms.name AS tag, … Read more

WP_query : Force last posts number over Reading Settings

In your loop, you must refer to $wp_query instance: <?php $args = array( ‘post_type’ => ‘post’, ‘posts_per_page’ => 3); $wp_query = new WP_Query($args); if ( $wp_query->have_posts() ) { while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?> <h4><a href=”https://wordpress.stackexchange.com/questions/266621/<?php the_permalink() ?>”><?php the_title(); ?></a></h4> <?php the_excerpt(); endwhile; } else { echo ‘No posts’; } wp_reset_postdata(); ?> Also, the_excerpt() … Read more

Detect inside a custom query the kind of post type to assign custom classes

The post_class function will do this for you automatically within the loop: <div <?php post_class(); ?>> which will add some combination of these classes: .post-[id] .[post-type] .type-[post-type] .status-[post-status] .format-[post-format] (default to ‘standard’) .post-password-required .post-password-protected .has-post-thumbnail .sticky .hentry (hAtom microformat pages) .[taxonomy]-[taxonomy-slug] (includes category) .tag-[tag-name] You can also get just the post type within the loop … Read more