This is actually quite easy to do: You will need to install a geolacation plugin that provides methods/functions to identify visitor country. N.B. A plugin only providing shortcodes will not work.
The only complicating factor is countries can be named in a variety of ways so 2 character ISO Country Codes have to be used for reliable visitor identifcation.
You could rename all your categories by adding an ISO code sufffix e.g. “Canada (CA)”; but association with visitors will be complicated and involve more code. So the example below assumes you will add either a 2nd category or a tag with the relevant uppercase ISO code e.g. “CA” to each post.
(tested and works for me)
-
Add a 2nd category (or tag) to each of your posts named as per 2 char ISO country code (ensure uppercase).
-
Install and activate the Category Country Aware plugin (as the author I’m familiar with it and so its the one I used it in the code below).
-
use an editor to create this plugin file (if using tags remove the 2 category lines, and uncomment the tag line as explained in comments)
<?php /* Plugin Name: Category for Visitor Description: only show posts for visitor's country on home page */ function my_visitors_category( $query ) { if (! class_exists('CCAgeoip')) return; $cat_id = get_cat_ID( CCAgeoip::get_visitor_country_code() ); // remove this line if using tag instead of 2nd cat if ( $query->is_home() && $query->is_main_query() ) { $query->set( 'cat', $cat_id); // remove this line if using tags // $query->set( 'tag', CCAgeoip::get_visitor_country_code() ); // uncomment this line if using tags for country ISO } } add_action( 'pre_get_posts', 'my_visitors_category' ); ?>
-
save as “visitor-category.php”; then Zip the file
-
Upload the zipped file via WP Dashboard->plugins->ad new->upload and activate