Question about querying posts

Here’s a very basic example that should do what you need it to do. Basically, we get all of the posts in your custom post type, then loop through them individually. When we find the one set with “promote_to_homepage,” we render some display and exit the loop: // Query for all posts in the post … Read more

query_posts pagination will always show identical content

Don’t use query_posts! As Eugene pointed out, you omitted the paged variable. However, even with that in you can get unexpected behaviour. As illustrated by the various questions we get on query_posts and pagination: query_posts and pagination, still stuck after much research Pagination throws 404 Pagination throws 404 error on custom taxonomy archive pages

Page title in post query

Your original code has a typo – the ampersand is used to separate arguments, so it should be: ‘posts_per_page=10&category_name=” . $post->post_title However, since this is a new query in addition to the default, a new instance of WP_Query should be used instead of query_posts: $args = array( “posts_per_page’ => 10, ‘category_name’ => $post->post_title ); $related … Read more

Related Posts: Notice: Undefined offset: 0

When you use wp_list_pluck on $terms what you get is an array where the key is the same as the value. If you dump the contents of $term_ids you’ll see something like: array( [42] => 42, [56] => 56 } The code in query.php tries to reference the zero index of the term_ids array, but … Read more

How to show the number of each post has been read in a while loop?

the_views() is not a built in function of WordPress. In fact, WordPress does not, by default, log the number of views of posts. There are probably several plug-ins that exist which add this functionality. The only one I have ever used (and its one I’ve never had any problems with) is WP-Postviews http://wordpress.org/extend/plugins/wp-postviews/.

How to place an image after Nth number of posts in query_posts

Multiple nested if statements can create a mess you might look into using a switch for this case. Something like: //your $args $the_switch_query = new WP_Query($args); while ( $the_switch_query ->have_posts() ) : $the_switch_query ->the_post(); $query_number = $the_switch_query->current_post + 1; //count them switch ($query_number){ case 1; echo “post one”; break; case 5; echo “post five”; break; … Read more

Exclude current sticky post

This should do: // Get sticky posts $sticky = get_option(‘sticky_posts’); rsort($sticky); $sticky = array_slice($sticky, 0, 3); // Check if current post is inside foreach ($sticky as $key => $value) if ($value === $GLOBALS[‘post’]->ID) { // … and remove unset($sticky[$key]); break; } $query = new WP_Query( array( ‘post__in’ => $sticky, ‘ignore_sticky_posts’ => 1, ) ); If … Read more

Display trimmed version of the_post()

First of all, never use query_posts, use WP_Query instead. To show 50 carachters of each one of the pages: <?php $args = array( ‘post_type’ => ‘page’, ‘post_parent’ => ‘6’, ‘order’=> ‘DESC’ ); $pages_returned = new WP_Query($args); while ( $pages_returned->have_posts()): $pages_returned->the_post(); ?> <div class=”page-excerpt”> <h1><?php the_title(); ?></h1> <?php the_excerpt(); ?> </div> <?php endwhile; ?> Now for … Read more