Hot answers tagged

1

There are several fatal assumptions here, and, a far better way to do it. 1. Declaring Functions inside Functions This will cause a fatal error in a future version of PHP, makes it super difficult to debug. But worst, it's completely unnecessary. This: function foo () { function bar () { // } bar(); } Should just be this: function bar(...


1

No you can't pass a callback function. Callback functions are PHP, so they can't be run in MySQL. For that to work it would need to query the results from the database first, but you'd get the wrong results because they're unsorted. You would need to use the posts_orderby filter to do this as part of the SQL query: add_filter( 'posts_orderby', ...


1

I guess my question is when are is_singular() && in_the_loop() both true? Note that both is_singular() and in_the_loop() point to the main WordPress query set via wp() (see Query Overview on WordPress Codex) which uses the global $wp_query variable. Secondly, we create/start a loop when we call have_posts() and the_post(), and only after that would ...


1

You may have already figured this one out, but here's two examples how to do this. In both of my examples I do the sorting first and rendering second. This keeps things cleaner and clearer in my opinion. Example 1 Loop queried posts and sort them into two helper arrays. If those arrays have posts, loop them through and render posts. $posts = ...


1

Using the built-in current_post you can easily separate the odd and even post. $args = array( 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 4, ); $loop = new WP_Query( $args ); if( $loop->have_posts() ): ...


1

Thus, you can glue two WP_Query queries with different post_type or other sorting data, for example, and then glue them by ids into one WP_Query query that will fully work - in order to preserve the sorting when gluing, you need to specify orderby => post__in my example is how it creates a new WP_Query cycle and can affect any other cycle for displaying ...


Only top voted, non community-wiki answers of a minimum length are eligible