Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

Retrieve language of a WordPress post using the WordPress API

Tiempo de lectura: < 1 minuto

html
Copy code

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// Get the post language using SQL query
global $wpdb;
$language = $wpdb->get_var($wpdb->prepare("
SELECT wp_terms.slug
FROM {$wpdb->posts} AS wp_posts
JOIN {$wpdb->term_relationships} AS wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
JOIN {$wpdb->terms} AS wp_terms ON (wp_term_relationships.term_taxonomy_id = wp_terms.term_id)
JOIN {$wpdb->term_taxonomy} AS wp_term_taxonomy ON (wp_terms.term_id = wp_term_taxonomy.term_id)
WHERE wp_posts.ID = %d
AND wp_term_taxonomy.taxonomy = 'language'
", $post->ID));
// Add the language to the response object
$response->data['language'] = $language;
return $response;
}
add_filter('rest_prepare_post', 'custom_rest_prepare_post', 10, 3);
// Get the post language using SQL query global $wpdb; $language = $wpdb->get_var($wpdb->prepare(" SELECT wp_terms.slug FROM {$wpdb->posts} AS wp_posts JOIN {$wpdb->term_relationships} AS wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) JOIN {$wpdb->terms} AS wp_terms ON (wp_term_relationships.term_taxonomy_id = wp_terms.term_id) JOIN {$wpdb->term_taxonomy} AS wp_term_taxonomy ON (wp_terms.term_id = wp_term_taxonomy.term_id) WHERE wp_posts.ID = %d AND wp_term_taxonomy.taxonomy = 'language' ", $post->ID)); // Add the language to the response object $response->data['language'] = $language; return $response; } add_filter('rest_prepare_post', 'custom_rest_prepare_post', 10, 3);
// Get the post language using SQL query
global $wpdb;
$language = $wpdb->get_var($wpdb->prepare("
    SELECT wp_terms.slug
    FROM {$wpdb->posts} AS wp_posts
    JOIN {$wpdb->term_relationships} AS wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
    JOIN {$wpdb->terms} AS wp_terms ON (wp_term_relationships.term_taxonomy_id = wp_terms.term_id)
    JOIN {$wpdb->term_taxonomy} AS wp_term_taxonomy ON (wp_terms.term_id = wp_term_taxonomy.term_id)
    WHERE wp_posts.ID = %d
    AND wp_term_taxonomy.taxonomy = 'language'
", $post->ID));

// Add the language to the response object
$response->data['language'] = $language;

return $response;
}
add_filter('rest_prepare_post', 'custom_rest_prepare_post', 10, 3);

In this code, we are using the rest_prepare_post filter to intercept the response from the WordPress API before it is sent to the client. Then, we execute the SQL query inside the custom_rest_prepare_post function to get the post language using the $wpdb class, which provides an interface to interact with the WordPress database.

Finally, we add the obtained language to the response object using the key 'language'.

Once you have added this code to your functions.php file or your custom plugin, when making a request to the WordPress API to get the posts, the 'language' field will be included in the response along with the other post data.

Now, when making the request to the WordPress API, it returns the following:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
"tags": [
1528,
1012,
826,
1525,
1527,
819,
1538,
813,
1526,
1524,
1543,
1541
],
"language": "es",
"tags": [ 1528, 1012, 826, 1525, 1527, 819, 1538, 813, 1526, 1524, 1543, 1541 ], "language": "es",
"tags": [
        1528,
        1012,
        826,
        1525,
        1527,
        819,
        1538,
        813,
        1526,
        1524,
        1543,
        1541
    ],
    "language": "es",
0

Leave a Comment