How to use a foreach loop with WP_Query

I prefer to use the get_posts() function when fetching content outside the regular loop in WordPress. I think it’s much cleaner than creating a new instance of the WP_Query class.

$posts = get_posts( $args );
foreach( $posts as $post ) : setup_postdata( $post );

// Do stuff

wp_reset_postdata(); endforeach;

Versus this:

$query = new WP_Query( $args );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();

// Do stuff

endwhile; endif;

There are times however when get_posts() can’t do all the things that WP_Query can. One example is that get_posts() can’t make a query based on a search string (for some reason).

$search_string = get_search_query();
// Won't work
get_posts( array( 's' => $search_string ) );
// Will work
new WP_Query( array( 's' => $search_string ) );

But there is still a way to loop through the posts without the ugly, regular WordPress loop, even when using WP_Query. You see, the WP_Query object has a get_posts() method!

$posts = new WP_Query( array( 's' => $search_string ) );
$posts = $posts->get_posts();

foreach( $posts as $post ) { /* ... */ }

Isn’t it nice to be able to use a foreach loop instead of the dirty and ugly standard WordPress loop? :)

Can’t save settings in WordPress?

I’ve stumbled across a very strange and annoying issue in WordPress 4.3. It happens when I’m trying to save/update settings on an ACF Options page. When the Save button is pressed, the JavaScript error below is thrown and the new settings can’t be saved.

Uncaught TypeError: Cannot use 'in' operator to search for 'length' in 0

I found the solution in this thread on the .org forum. The problem has to do with jQuery 1.11.3 and the solution is to simply replace wp-includes/js/jquery/jquery.js with jQuery 1.11.0 for example. Any other version besides 1.11.3 or 2.1.4 would do the trick!

Sublime packages I use

I’ve been using Sublime Text as my code editor for about four or five years now. Since version 3 is currently in beta and has been for a while, I’ve made the switch from version 2 to 3.
This process made me discover which packages I’m using the most or misses the most when it’s not installed yet.
Here it goes, a classic list of Sublime packages you can’t live without™.

Peacock color scheme

I’m also a geek when it comes to color schemes. Without a sexy color scheme for my editor, I can’t get any work done. Peacock has been a favourite for quite some time, but since my switch to ST3, Material Theme is a requirement for being productive.


I also like to have a bit larger font size and some spacing between lines. These are my settings in Sublime:

    "theme": "Material-Theme.sublime-theme",
    "color_scheme": "Packages/Material Theme/schemes/Material-Theme.tmTheme",
    "font_face": "Menlo",
    "font_size": 14,
    "line_padding_top": 3,
    "line_padding_bottom": 3