How to get The Loop working with $wpdb->get_results()

Turns out I didn’t follow the instructions in the manual to the letter: I seem to have declared global $post too early. I don’t entirely see through this but it works fine now using this code: $eventsPosts = $wpdb->get_results($querystr, OBJECT); if ($eventsPosts) { global $post; foreach ($eventsPosts as $post) { setup_postdata($post); the_title(); // or whatever … Read more

WordPress: Loop Help

You’re using query_posts() for your featured-post custom loop. As the Codex says: don’t do that. The query_posts() function is intended to modify the Primary Loop. If you need a custom loop, use e.g. $my_custom_query = new WP_Query( $args ). See here for a good break-down of custom-loop options.

Both a page loop and posts loop on the same page

Change: foreach ($posts_array as $monster_news) { setup_postdata($monster_news); To: foreach ($posts_array as $post) { setup_postdata($post); It seems setup_postdata() actually doesn’t modify $post itself (which was news to me). PS also loose empty , in array… I am not sure how it actually works for you with that.

Added if statement to loop

Try the following: if ( have_posts() ) { while( have_posts() ) { the_post(); if ( ‘reviews’ === get_post_type( get_the_ID() ) { echo ‘I am a post of the post type ”reviews“’; // We’re done here, continue to next post continue; } // Do other stuff } } EDIT In case you’re not sure if you … Read more

How do I automate multiple category loops?

You get all categories with: $categories = get_categories( array ( ‘fields’ => ‘ids’ ) ); This returns an array with all categories. Then you should use get_posts() or a new WP_Query, because query_posts() has many side-effects and should not be used anymore. Sample code, as illustration: $categories = get_categories( array ( ‘number’ => 5 ) … Read more

Combine the results of two loops [closed]

Yes it is. First you have to combine the results in one array: $all_posts = array_merge( $related->posts, $features->posts ); Now, let’s sort the array items by date: usort( $all_posts, function( $a, $b ) { return strcmp( $b->post_date, $a->post_date ); } ); Finally, do the loop: global $post; foreach ( $all_posts as $post ) { setup_postdata( … Read more

Why does `inclusive` not work in this wordpress loop?

Edit 3 Now, combine Edits 1 and 2: ‘date_query’ = > array( array( ‘before’ = > strtotime( get_the_date() ), ‘inclusive’ = > true, ) ) Edit 2 You don’t have your inclusive parameter inside the correct array. Change this: ‘date_query’ = > array( array( ‘before’ = > get_the_date() ), ‘inclusive’ = > true, ) …to … Read more