How to sort a loop after most viewed

Having taken a quick look at the wp-postviews documentation, it seems you can change query_posts( ‘posts_per_page=5’ ); to query_posts( ‘posts_per_page=5&v_sortby=views&v_orderby=desc’ );, but I haven’t tested that. Give it a shot and see how it turns out. A cautionary note on doing this: What often happens when you sort by views is that the first page … Read more

How to check all items in a loop have post thumbnail?

Before you do your output, do something like this: $no_thumbnails = true; foreach( $shows_sales as $p ) { if( has_post_thumbnail( $p->ID ) ) $no_thumbnails = false; } Then in your loop you can do something like this: <?php if( $no_thumbnails === true ) : ?> <!– Your output here –> <?php endif; ?>

Help with if and else statement

What you’re gonna want to do is completely rewrite that super-complex structure you have to something like this: <?php if( $my_query->have_posts() ) : ?> <?php while( $my_query->have_posts() ) : $my_query->the_post(); ?> <!– your output here –> <?php endwhile; else : ?> <!– your failure output here –> <?php endif; ?>

Ascending order has strange effects on wordpress loop

It seems the problem is solved by adding ‘posts_per_page’ => -1 which forces WordPress to show all posts matching the parameters. Here’s my final code: array ( ‘post_type’ => ‘symposia_program’, ‘days’ => $progDay, ‘meta_key’ => ‘class_number’, ‘orderby’ => ‘meta_value_num’, ‘order’ => ‘ASC’, ‘posts_per_page’ => -1 );

Displaying member join date on page

You can use the WP_User_Query class to query on user meta, however, your specific example of querying for a list of users with a join date matching a specific month and from any year requires a query more complex than what you can do out of the box with the WP_User_Query class. First, it’s important … Read more

loop inside a loop : search for posts in the same category

the_ID() prints the current post’s ID. You need to use get_the_ID() to return it. Also, get_the_category returns an array of category objects – one foreach category term the post belongs to. The ‘category’ attribute expects a category term ID. So you need to pick a category object, then obtain its ID: $cats = get_the_category(); $cat_obj … Read more

Avoid WP_Query’s duplicate posts with taxonomies

Pass the post ID from the first query as a post__not_in parameter to exclude it from the second query. $nature_loop_1 = new WP_Query( array ( ‘category_name’ => ‘nature’, ‘tax_query’ => array ( array ( ‘taxonomy’ => ‘highlight’, ‘field’ => ‘slug’, ‘terms’ => ‘sidebar-highlight’, ‘operator’ => ‘IN’ ) ), ) ); $exclude = $nature_loop_1->post->ID; $nature_loop_2 = … Read more

Exclude current post ID from loop in sidepbar.php

First, do not use query_posts() for secondary loops. The query_posts() function is intended only to modify the primary loop query. Use WP_Query() or get_posts() for secondary loop queries. Also, showposts is deprecated. Use posts_per_page instead. Let’s use WP_Query(), as it will be the most analogous to your current implementation: <?php // First, let’s eliminate some … Read more