0

Can I set countrywise post view? Think you are from Russia and you’ll see from Russia category post only, and another user from Japan, he’ll see Japan posts only on the same page, provably in homepage. How can I do this? Thanks

Also, how can I set random post-show rather than selecting a specific post category? Is this possible?

0

It's possible, let's say you want to affect all post queries like this, you will need to get the country of origin and then change the query. Given you have two categories, something like Russian news and Japanese news, you could do it basically like this:

/**
 * Returns origin country of request if available
 *
 * @return mixed false (not available to retrieve) or country code (string)
 */
function get_country_of_visitor() {
    $data = json_decode(file_get_contents('http://ip-api.com/json/' . $_SERVER['REMOTE_ADDR']), true);

    // Return countryCode only if available in
    if (isset($data['countryCode'])) {
        return $data['countryCode'];
    }

    return false;
}

/**
 * Wrapper function for get_country_of_visitor() and condition
 * @uses get_country_of_visitor()
 * @return bool Is japanese?
 */
function is_japanese() {
    $country = get_country_of_visitor();
    return $country === 'JP';
}

/**
 * Wrapper function for get_country_of_visitor() and condition
 * @uses get_country_of_visitor()
 * @return bool Is russian?
 */
function is_russian() {
    $country = get_country_of_visitor();
    return $country === 'RU';
}

/**
 * Change query when needed
 *
 * @uses https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
 */
function wpse331934_alter_query($query) {

    $tax_query = false;

    // Conditionally set tax_query
    if ( is_japanese() ) {
        $tax_query = [
            [
                'taxonomy' => 'category',
                'field' => 'name',
                'terms' => 'Japanese news'
            ]
        ];
    } else if ( is_russian() ) {
        $tax_query = [
            [
                'taxonomy' => 'category',
                'field' => 'name',
                'terms' => 'Russian news'
            ]
        ];
    }

    // Set query for category if specified
    if ( $tax_query ) {
        $query->set( 'tax_query', $tax_query );
    }

    return $query;
}

add_action( 'pre_get_posts', 'wpse331934_alter_query' );

As you can see wpse331934_alter_query in pre_get_posts hook alters the query by adding tax_query if any of the conditions is_japanese() or is_russian() returns true.

If, as you say, you want to that for example only on homepage, you can add any other condition, for homepage you could use is_front_page() in the pre_get_posts hook.

/**
 * Change query when needed
 *
 * @uses https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
 */
function wpse331934_alter_query($query) {

    $tax_query = false;

    // Conditionally set tax_query
    if ( is_front_page() && is_japanese() ) {
        $tax_query = [
            [
                'taxonomy' => 'category',
                'field' => 'name',
                'terms' => 'Japanese news'
            ]
        ];
    } else if ( is_front_page() && is_russian() ) {
        $tax_query = [
            [
                'taxonomy' => 'category',
                'field' => 'name',
                'terms' => 'Russian news'
            ]
        ];
    }

    // Set query for category if specified
    if ( $tax_query ) {
        $query->set( 'tax_query', $tax_query );
    }

    return $query;
}

add_action( 'pre_get_posts', 'wpse331934_alter_query' );

Note that is_front_page() comes as first condition, that way you will save yourself one, potentially two calls of get_country_of_visitor() if you are not on the front page. Anyway, saying this, get_country_of_visitor() in this example relies on 3rd party (which can change anytime) and also sends remote request. Therefore i would not use it like this in production. Hopefully you will be able to come up with different way to declare the origin of request somehow without having to request from third party every time your page is requested.

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.