Using one WP_Query object within the loop of another WP_Query object
Go through each loop, storing the post object in an array. Then at the end combine them. (This is completely untested) https://gist.github.com/1762204
Go through each loop, storing the post object in an array. Then at the end combine them. (This is completely untested) https://gist.github.com/1762204
Surprisingly, WordPress does not make it very straightforward. Even though lots of people have had similar questions on the wordpress.org forums, I believe yours is unique in that you want to keep the order and sort. This is by no means quality code, but does exactly what you’re looking for. class WP_Query_Custom extends WP_Query { … Read more
Actually, you’re a mistaken on a couple of points here. 1. Don’t use query_posts() This function is meant for modifying an existing query, not performing a standalone query. You should either use get_posts() or a new instance of the WP_Query() class to perform your query for the custom loop. Here’s some further reading on query_posts(): … Read more
You should use all of the goodness of the wpdb object. #Create a new WPDB object for your custom DB $foo_db = new wpdb(‘username’,’password’,’database’,’localhost’); $query = (‘SELECT * FROM custom_table’); #Use the get_results method – You can return OBJECT(default), OBJECT_K, ARRAY_A, ARRAY_N $results = $foo_db->get_results($query, OBJECT); foreach($results as $row){ $user = new WP_User($row->user_id); } You … Read more
You’re initially submitting a POST request via a form with the sort parameters. When you click links to additional pages, you’re just sending a GET request for the next page without those original POST vars, so they don’t carry over to the additional pages and aren’t picked up by your if(isset($_REQUEST[‘sort’])). Probably the simpler way … Read more
You need to join the post_meta table twice to the posts table, once for each meta value: SELECT p.ID id, p.post_title title, country.meta_value country, region.meta_value region FROM {$wpdb->posts} p JOIN {$wpdb->postmeta} country ON p.ID = country.post_id AND country.meta_key = ‘search_country’ JOIN {$wpdb->postmeta} region ON p.ID = region.post_id AND region.meta_key = ‘search_region’ WHERE p.post_status=”publish” AND p.post_type=”films” … Read more
In every template file of your theme, even in the header.php you can put <?php $term = null; $btn = __(‘Search’); if ( is_category() ) { $term = get_queried_object(); } ?> <form method=”get” id=”search_form” action=”<?php echo home_url(); ?>”> <div> <input type=”text” value=”” name=”s” id=”s” /> <?php if ($term) { $btn = sprintf( __(‘Search in %s’), … Read more
Here is a rough filter: add_filter( ‘template_include’, function($template) { global $wp_query; if (1 == $wp_query->found_posts) { global $wp_query; $type = $wp_query->get(‘post_type’) ?: false; $template_type = $type ? ‘single-‘ . $type. ‘.php’ : ‘single.php’; if ( locate_template($template_type) ) { return locate_template($template_type); } elseif ( $type && locate_template(‘single.php’) ) { return locate_template(‘single.php’); } } return $template; } … Read more
How about this? /** * Convert query string to meta query. */ function query_string_to_meta_query( $query_string ) { $meta_query = array( ‘relation’ => ‘AND’ ); foreach ( wp_parse_args( $query_string ) as $key => $value ) { $meta_query[] = array( ‘key’ => $key, ‘value’ => $value ); } return array( ‘post_type’ => array( ‘post’, ‘custom’ ), ‘meta_query’ … Read more
Headers are sent long before you try to alter them. Headers are sent by actions associated with get_header(), so by the time your code executes, it is too late to alter the headers. You can demonstrate this with a simple experiment. Try each of the following: get_header(); status_header( 404 ); and status_header( 404 ); get_header(); … Read more