0

Am recently trying to learn the framework aspect of WordPress,so i want to build a user homepage that only displays posts from other registered users, but the posts that will be displayed will have to be posts from users who are from the same location as the user viewing such posts.

For instance, in a pool of users from different countries, users will only see posts of other users from his/her own country.

So how do i go about achieving it, what template tag or functions should i use?

Will be very happy if anyone can help me in getting it done. Thanks in advance for your help.

  • That is too broad. you are looking for template tags and functions to do exactly what? – Mark Kaplun Apr 11 '16 at 5:48
  • @MarkKaplun, for instance, as a user on this community, having access to only posts from users that are from my country is a better way of explaining the question. so if i can get an example code of how create a query alongside the WP loop, i'll be very happy sir.....pls understand that am trying to learn. – femi jeffery Apr 11 '16 at 10:19
0

This is more of a logical problem.

Lets say you stored location (City/address/Lat-long(Best would be Lat/long) of user in a custom field (There should be a standard way to assign locations to users throughout the site). you want to query posts from users which are near this particular user.

  • Get custom field value of location for this user.
  • Convert this location to Lat-long (So that you can query google map)
  • Find locations near this particular location by using Google Maps API. Use this page to know how to do that. Also you can find many other ways of doing this by googling.
  • Get results from google and convert them in that standard form you are using on your site.
  • Now you can query WordPress (Using meta query) to get all the users living in cities you just found.
  • After getting users you can get posts for each user.

If you are trying to get posts from users of same country that would be quiet easy. You can use meta query for this task. Try below arguments in WP_Query:

$args = array(
    'post_type'=>'post',
    'meta_query'=>array(
        'key'=>'country',
        'value'=> $country, //$country is a variable with country's name
        'type'=>'CHAR'
        'compare'=>'='
        ),
);
$query = new WP_Query($args);

To learn more about meta you can use WordPress CodeX.

To see all arguments of WP Query use this link.

  • Very very helpful of you sir, breaking it down. But that's kind of a bit complex for me, if there's a way to achieve it by writing query within WordPress loop template without having to involve the google map API, that will be very helpful. But for now, will read up the link in your answer. once again Thanks for the response. – femi jeffery Apr 11 '16 at 10:47
  • I have edited my answer in case you only want to get posts from users of same country – Deepak jha Apr 12 '16 at 21:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.