Tag Info

Hot answers tagged

3

Build an array of categories: foreach ($categories as $category) { $cat[] = $category->cat_ID; } Then pass that to the query: $args=array( 'category__in' => $cat, 'order' =>DESC, 'post__not_in' => array($post->ID), 'posts_per_page'=>2, ); In other words, put the query after the foreach, not inside of it.


1

You are interfering with the main query/loop, which is bad - there is nothing more to say. You can do a custom loop, see the codex article The Loop to start with, or use the pre_get_posts hook, if you want to do something other then the usual behaviour. If you need to toy with $wp_query, for example to fix the pagination on custom loops, then restore it ...


1

You can use the found_posts property, which is the total number posts for the current query: echo $the_query->found_posts;


1

Firstly, you are invoking an infinite loop, which causes the memory exhaustion. To avoid it, put the following at the beginning of your function: // avoid infinite loop remove_action( 'pre_get_posts', __FUNCTION__ ); It makes sure the you are not hooking it into pre_get_posts over and over again, re-initiating your get_posts() call over and over again. ...


1

As usual it's most reliable to dump the resulting SQL query and see: SELECT wp_posts.ID FROM wp_posts WHERE 1=1 AND (((wp_posts.post_title LIKE '%keyword%') OR (wp_posts.post_content LIKE '%keyword%'))) AND wp_posts.post_type = 'post' AND ((wp_posts.post_status = 'publish')) ORDER BY wp_posts.post_date DESC LIMIT 0,5 The only two things ...


1

Take a look at freegeoip.net. I used it like this with jquery, for example. $.get( "http://freegeoip.net/json/", 'jsonp', function( data ) { country = data['country_code']; if( country == 'US' ){ // show your content }; });


1

You can use pre_get_posts to modify the main query, but you will need to create new queries for the other searches. Use get_query_var('s') to get the search terms and create new WP_Query objects to perform your search. Without more information it is hard to provide much more detail to the answer.



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