Modify Search Query if original Query gave no results

You should create a custom query inside if. if ( $total_results == 0 ) { // Create a new query with the terms ‘Find Us’ $query = new Wp_query(‘s=Find Us’); if ($query->have_posts()): while ($query->have_posts()):$query->the_post(); //whatever you want to do with the result endwhile; endif; wp_reset_postdata(); } You can refer Creating a search page and Wp_query … Read more

Display Current Post’s Position in Custom Taxonomy

If the post order reflect the publishing date (and note that publishing date also takes into account hour, minute and second of publication) than is pretty easy to get the order, using a query that return only post ids and then array_search to find current post id index: $query = new WP_Query( array( ‘TAXONOMY’ => … Read more

WP_Query fails despite having 1 post

T a look at how to properly construct a query with WP_Query. $args = array( ‘post_type’ => ‘cameras’, ‘posts_per_page’ => 5, ); $the_query = new WP_Query( $args ); // The Loop if ( $the_query->have_posts() ) { echo ‘<ul>’; while ( $the_query->have_posts() ) { $the_query->the_post(); echo ‘<li>’ . get_the_title() . ‘</li>’; } echo ‘</ul>’; } else … Read more

Query only the posts with a post format of “audio”

Before we continue, I must point out this: $i=1; $temp = $wp_query; $wp_query = null; $wp_query = new WP_Query(); $wp_query -> query(‘post-format-audio’.’&paged=’.$paged) This is exactly the same as: query_posts( ‘post-format-audio’.’&paged=’.$paged ); Which is terrible. When people advise against using query_posts, it’s not just the function that’s bad, it’s what it does. Thankfully someone told you … Read more

Change query from cat id to slug or name?

Lets start by removing the query_posts() from your code since you already have it’s better brother new WP_Query. Then what you will need is to use the category_name param on your WP_Query. You should always search on the WP_Query page before you ask here, you can find more referencers there. Here is how @speedypancake resolved … Read more

Search posts missing a particular custom field

Try using ‘NOT EXISTS’ instead of ‘NOT LIKE’ and remove the value ( it’ll be removed by WP with NOT EXISTS ): $args = array( ‘post_type’ => ‘post’, ‘posts_per_page’ => 5, ‘meta_query’ => array( array( ‘key’ => ‘verified’, ‘compare’ => ‘NOT EXISTS’ ) ) ); $query = new WP_Query( $args ); http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

Sort WordPress Posts Meta value by Week not Day

I agree with Rarst about the WEEK function, but be sure to also use YEAR as well to order the posts. So your order by would look something like this: ORDER BY YEAR($wpdb->posts.post_date) DESC, WEEK($wpdb->posts.post_date) DESC, $wpdb->postmeta.meta_value+0 DESC Of course, this is still pulling all posts, so you might also want to just limit the … Read more