WP_Query posts by distance based on LAT & LNG in Database

You want to use the Haversine formula http://en.wikipedia.org/wiki/Haversine_formula This plugin might be of interest to you: http://wordpress.org/extend/plugins/wp-geoposts/ Provides WP_GeoQuery an extended WP_Query class for doing distance based and geo-aware queries. it uses the Haversine formula here: http://plugins.svn.wordpress.org/wp-geoposts/trunk/query.php

How to display 7 most recent days of posts?

You can do all of this outside the template, via the pre_get_posts action and a filter on posts_where. The first function checks if it’s the main query and the home page, if so it applies a filter to the query’s posts_where clause. The posts_where function limits post selection to today’s date minus 7 days, then … Read more

Make WP_Query more efficient?

you can try some of these extra parameters: ‘update_post_term_cache’ => false, ‘update_post_meta_cache’ => false, ‘cache_results’=>false, ‘no_found_rows’ => true, ‘posts_per_page’=>1, ‘post_status’=’publish’, ps: you can measure the speed of your query when you are testing it with microtime() http://php.net/manual/en/function.microtime.php Edit: Here is a another way: function count_votes($user_id=1, $question_id=1, $post_type=”vote”, $meta_key=’comp_question’){ global $wpdb; $sql = “SELECT count(*) FROM … Read more

Setting get_queried_object

As per its laconic source: function get_queried_object() { global $wp_query; return $wp_query->get_queried_object(); } This function retrieves object from main query. As such it is affected by anything that changes main query. From your list that would be query_posts() (reason number umpteen it should not be used).

How to query a custom post type with a taxonomy filter but display post type archive page?

The simplest way is, in all honesty, to hook into wp_title and change things, and just be aware that the warnings are there but not showing in production (because you’ve property configured your server). What follows is a kind of hacky solution. If you take a look at get_queried_object… <?php function get_queried_object() { global $wp_query; … Read more

Custom Search Result Page displaying only 1 result.

Change <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> to <?php if ($allsearch->have_posts()) : ?> <?php while ($allsearch->have_posts()) : $allsearch->the_post(); ?> because otherwise you don’t utilize your custom query. See WP_Query Usage. Also call wp_reset_query(); after the Loop. See wp_reset_query Description. Actually, you don’t need wp_reset_query(); because you use custom WP_Query object … Read more

Query “Category A” + 1 post from “Category B” – how?

I don’t see a way to do this with WP_Query alone. However… function posts_where_add_post_wpse_96030($where) { global $wpdb; return $where.” OR {$wpdb->posts}.ID = 1″; } add_filter(‘posts_where’,’posts_where_add_post_wpse_96030′); $query = new WP_Query( ‘cat=9’ ); That will alter the query everywhere it runs so you will need to add conditions to the callback so that $where is only altered … Read more

WP_Query returns more results than expected

There is no post_title in WP_Query‘s parameter list. You need to use name, which accepts a slug not the actual post title. name (string) – use post slug. It looks like all you are doing is getting an ID. That is a very heavy query if that is all you need. You can alter the … Read more