Enabling users to control custom loop’s query

User meta is the way to go friend! When you register the user via whatever you’re using, save this preference as user meta for this user. Then when you render the template, get the current user id, and get this user meta before the loop and use it in the loop.

Output from Meta Box Array

Add meta data as “flat” data to the post The actual problem is, that your meta data comes back as array (with a single entry), when calling get_post_custom( $id ); for your post. Here’s a simple function, that adds all meta data attached to a post to your post object. /** * Merges custom/meta data … Read more

Efficiently sort only certain categories by custom field

I would suggest to hook onto pre_get_posts and alter the query, rather than firing off your own in the template (it keeps your template tidy, and you’ll avoid doubling your database queries!). Below demonstrates this, plus a bonus settings field in the admin (under “Reading”) to select the categories that “special sorting” applies to – … Read more

Issue with front page navigation after upgrading to 3.4

It stopped to work with WP 3.4 because there was fixes to handle_404(), and You are using wrong way on homepage… instead of creating new query, modify main query. Changeset: http://core.trac.wordpress.org/changeset/19892 Sample code: function my_query_for_homepage( $query ) { if( $query->is_main_query() && $query->is_home() ) { $query->set( ‘post_type’, array( ‘post’, ‘gaming’, ‘entertainment’, ‘tech’, ‘breakroom’, ‘podcasts’, ‘off-grid’ ) … Read more

How can I improve the performance of this query_posts loop?

Not sure how dramatic this’ll be, but it should improve nontheless: <?php /** * You can try forcibly splitting the query (grabbing IDs *then* all fields) * Works well for large post results. (available WP 3.4+) */ add_filter( ‘split_the_query’, ‘__return_true’ ); $posts = get_posts( array( ‘post_type’ => ‘listings’, ‘posts_per_page’ => -1, /* Disable term caching … Read more

Transient api Caches confused

“This code works” – but it doesn’t! If you’re gonna cache paginated posts, you’ll need to store them chunked: if ( ! $my_paged = absint( get_query_var( ‘paged’ ) ) ) $my_paged = 1; if ( ! $my_query = get_transient( “recent_$paged” ) ) { $my_query = new WP_Query( “cat=3&posts_per_page=5&paged=$my_paged” ); set_transient( “recent_$paged”, $my_query, 60 * 60 … Read more

Display Post Author Link above the loop

It make use of global variable $post to get author id of current post. Using that this function returns the author url. Note – Make sure you put this in condition ( is_single() ) so it only print author url on single.php. <?php echo get_author_posts_url( $post->post_author ); ?> Update – <?php $username = get_userdata( $post->post_author … Read more