Display only certain posts based on visitor’s country?

Is it theoretically possible to have the following feature?

Each post has a country code. When a visitor comes in, the system will determine the visitor’s country code. Then when the page renders, only the post with that country code is displayed. This need to also affect the widgets/site search etc.

Read More

I was asked to evaluate this. I think this would lead to if statements all over the place, in the loop, and I need to modify the widgets too. All in all I think it is doable but difficult and would make the whole code-base unmaintainable. However I want to get some insight in this, since I only have experience with themes, not plugin development.

Also I am not sure how to tag this more appropriately. Any suggestion is welcomed.

UPDATE:
I took a look at the ‘Country Filter’ plugin, it’s nice but does filtering in the content level not the post level. I am also looking at add_filter(request,...) to see if this can be done at the root.

Related posts

Leave a Reply

2 comments

  1. Is it theoretically possible to have the following feature?

    Yes theoretically there are several Geo location API’s out there, the reliable ones you have to pay for, such as MaxMind.

    The latter part sounds strange, if you want the same content, then just use language files.

    If for some crazy reason you want to serve up different content per country, it would be quite the undertaking, you most likely would want to create your own API or custom page per country, you dont want a billion if statements.

  2. Thanks Sisir! It is pretty possible, at least I think I am 2/3 way there. By using pre_get_post action I filtered out posts successfully. This is action hook is more powerful than add_filter(request,...). There are still things I need to fix such as filtering out recent comments links related to target posts, and single post page that still displays, but I think they should be possible.