why the same code got different results when using query_posts in functions.php and index.php

In your first code example, you hook into the pre_get_posts action, which fires during the main query. But instead of manipulating that query, you trigger another one using query_posts(), which fires the hook again, and you end up with an infinite loop. With the second code example, you’re just overriding the main query after it’s … Read more

post-per-page and offset not working

Your query args are messed up. Posts per page and offset params are not part of the tax_query array. Check out the example in the Codex: $args = array( ‘post_type’ => array(‘your_cpt’), ‘posts_per_page’ => 3, ‘offset’ => 3, ‘tax_query’ => array( ‘relation’ => ‘OR’, array( ‘taxonomy’ => ‘brands’, ‘terms’ => array(‘iHOME’), ‘field’ => ‘slug’, ) … Read more

Pagination broken on is_front_page()

According to the WP_Query docs: Pagination Note: Use get_query_var(‘page’); if you want your query to work in a Page template that you’ve set as your static front page. The query variable ‘page’ also holds the pagenumber for a single paginated Post or Page that includes the Quicktag in the post content. Display posts from current … Read more

How can I access specific posts brought back by query_posts?

Why not just use a counter and then conditional logic? $i = 0; while($loop->have_posts()) : $loop->the_post(); if($i === 0){ echo ‘<div class=”a”>’.other_stuff().'</div>’; } elseif($i === 1){ echo ‘<div class=”b”>’.other_stuff().'</div>’; } else { echo ‘<div class=”default”>’.other_stuff().'</div>’; } $i++; endwhile;

3 random images from custom post type, each in a div with a diffrent class

Getting different classes in the divs is relatively simple and mostly not a WordPress question, but there are WordPress bits to it that need comment. Truncated code reformatted for legibility: $classes = array( ‘side left’, ‘middle’, ‘side right’ ); $qry = new WP_Query( array( ‘post_type’=> ‘portfolio’, ‘posts_per_page’ => 3, ‘orderby’ => ‘rand’, ‘order’ => ‘ASC’ … Read more

get_posts output always same post

This is because you are not passing the parameters in the correct way, taxonomy param needs to be passed under tax_query. Please replace the $args with the below:– $args = array( ‘posts_per_page’ => 4, ‘tax_query’ => array( array( ‘taxonomy’ => ‘tag’, ‘field’ => ‘slug’, ‘terms’ => ‘pp’ ) ) ); For more details check the … Read more