Skip Navigation

This thread is resolved. Here is a description of the problem and solution.

Problem:
The client is building a headless application and needs REST API documentation for handling posts, custom posts, taxonomies, and custom taxonomies with language filtering.

Solution:
If you're experiencing this, we recommend using the WordPress REST API to retrieve taxonomy terms. To get the list of terms for a taxonomy, use the following endpoint:

https://yoursite.com/wp-json/wp/v2/{taxonomy-slug}

To get the list of terms for a taxonomy in a specific language, append the language parameter:

https://yoursite.com/wp-json/wp/v2/{taxonomy-slug}?lang={lang_code}

For example, to get categories in German:

https://yoursite.com/wp-json/wp/v2/categories?lang=de

Refer to the WordPress REST API Taxonomies documentation for more details.

To retrieve posts from a category based on language, you can use the

wpml_object_id

filter hook within a loop to get the translated post ID. Here's an example code snippet:

$args = array(
    'post_type' => 'post',
    'tax_query' => array(
        array(
            'taxonomy' => 'category',
            'field' => 'slug',
            'terms' => 'bob',
        ),
    ),
);
// The Query.
$the_query = new WP_Query( $args );
 
// The Loop.
if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
 
        $my_post_id = get_the_ID();
        $my_post_id = apply_filters( 'wpml_object_id', $my_post_id, 'post', false, 'fr' ); //get the post ID in French
 
        echo '<li>' . esc_html( get_the_title( $my_post_id ) ) . '</li>';
    }
    echo '</ul>';
} else {
    esc_html_e( 'Sorry, no posts matched your criteria.' );
}
// Restore original Post Data.
wp_reset_postdata();

For more information, refer to the wpml_object_id hook documentation and the WP_Query Taxonomy Parameters documentation.

Please note that custom code is beyond our support policy. If you need further assistance with custom code, consider contacting one of our certified partners.

If this solution doesn't look relevant, please open a new support ticket.

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

Tagged: 

This topic contains 9 replies, has 2 voices.

Last updated by Long Nguyen 1 year, 5 months ago.

Assisted by: Long Nguyen.

Author Posts
December 22, 2023 at 6:21 pm #15121425

sureshP-3

I am trying to build a headless application and needs the REST API documentation for posts, custom post, taxonomy, custom taxonomy etc

I got input only for posts rest api

I need more details for taxonomy rest api

December 25, 2023 at 6:43 am #15125473

Long Nguyen
WPML Supporter since 02/2022

Languages: English (English )

Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Hi Suresh,

Thank you for contacting WPML support, I'd be happy to help you with this issue.

I remember you had the same question in the past and I've already replied to your question:
https://wpml.org/forums/topic/wordpress-rest-api-documentation-for-posts-custom-post-custom-taxonomy/#post-15013659

If you want to get the list of terms of taxonomy, you can use this URL:
hidden link{taxonomy-slug}
If you want to get the list of terms of taxonomy in a second language, you can use this URL:
hidden link{taxonomy-slug}?lang={lang_code}

For example: the list of categories in German language:
hidden link

You can test this feature on a sandbox site, here is the URL to access the site directly
hidden link

Refer to WordPress documentation https://developer.wordpress.org/rest-api/reference/taxonomies/

Look forward to your reply.
Thanks

December 25, 2023 at 4:30 pm #15127271

sureshP-3

Could you share me the WP Query or hook to retrieve all posts for a category based on language code ?

December 26, 2023 at 2:30 am #15127809

Long Nguyen
WPML Supporter since 02/2022

Languages: English (English )

Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Hi,

You can follow the WordPress documentation to create a query to get posts from a category. In the loop, use the WPML filter hook "wpml_object_id" to get the translation post ID in a specific language and output post info based on the ID. Here is an example:

$args = array(
	'post_type' => 'post',
	'tax_query' => array(
		array(
			'taxonomy' => 'category',
			'field' => 'slug',
			'terms' => 'bob',
		),
	),
);
// The Query.
$the_query = new WP_Query( $args );

// The Loop.
if ( $the_query->have_posts() ) {
	echo '<ul>';
	while ( $the_query->have_posts() ) {
		$the_query->the_post();

		$my_post_id = get_the_ID();
		$my_post_id = apply_filters( 'wpml_object_id', $my_post_id, 'post', false, 'fr' ); //get the post ID in French

		echo '<li>' . esc_html( get_the_title( $my_post_id ) ) . '</li>';
	}
	echo '</ul>';
} else {
	esc_html_e( 'Sorry, no posts matched your criteria.' );
}
// Restore original Post Data.
wp_reset_postdata();

Refer documentation
https://wpml.org/wpml-hook/wpml_object_id/
https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters

Note: this is a custom code and I would like to inform you that helping you with custom code, is out of the scope of WPML. If you are not able to accomplish this, I would recommend you contact one of our certified partners who will be more than happy to help you with this. In this link, you will find a list of our certified partners: https://wpml.org/contractors/

Thanks.

December 26, 2023 at 12:53 pm #15130041

sureshP-3

Don't we have something like this

$temp = get_posts( array(
'post_type' => 'post',
'numberposts' => -1,
'post_status' => 'publish',
'lang' => 'en',//Change the language code here
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $slug
)
),
));

I don't want to loop the result, I want to query only selected condition post so i can return to my rest api

December 27, 2023 at 1:32 am #15131013

Long Nguyen
WPML Supporter since 02/2022

Languages: English (English )

Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Hi,

I'm afraid that there isn't an argument "lang" to set the language in the list of arguments like that. You can use the filter hook "wpml_object_id" in the loop, as I mentioned above, to get the translation post ID in a specific language.

Thanks.

December 28, 2023 at 9:24 am #15133947

sureshP-3

That hook is not working as far i checked

December 29, 2023 at 8:30 am #15136046

Long Nguyen
WPML Supporter since 02/2022

Languages: English (English )

Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Hi,

Can you please replicate the issue on the sandbox site that I shared above? Then let me know steps to replicate it.
hidden link

Look forward to your reply.
Thanks

December 29, 2023 at 3:02 pm #15136980

sureshP-3

I have uploaded a test plugin it was somehow cache i believed so and its working now with that hook method. can you guide me how to retrieve a single post based on language??

January 2, 2024 at 1:32 am #15139943

Long Nguyen
WPML Supporter since 02/2022

Languages: English (English )

Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Hi,

When you pass the post ID to the query, you can also use the filter hook "wpml_object_id" to get the translation post ID in a second language, following the WordPress documentation
https://developer.wordpress.org/reference/classes/wp_query/#post-page-parameters

Please note: this is a custom code and I would like to inform you that helping you with custom code, is out of the scope of WPML. If you cannot accomplish this, I recommend you contact one of our certified partners who will be more than happy to help you with this. In this link, you will find a list of our certified partners: https://wpml.org/contractors/

Thanks.