-
I made this thread (https://support.rankmath.com/ticket/rankmath-wordpress-rest-api/?view=all&unm=321289) some time ago, and now i have a follow up question. Does this look correct (code below)? Is that how i shall change the postmeta data?
And also, how to make a POST call for defining the “Primary term” (primary category, which defines how the breadcrumbs looks like (see here: https://support.rankmath.com/ticket/how-to-make-term-primary-via-api/)). The primary term is a rankmath field, right?
Script:
import requests
# Define the WordPress site URL
WORDPRESS_URL = ‘https://your-wordpress-site.com’# Define the Rank Math metadata keys
RANK_MATH_TITLE_KEY = ‘rank_math_title’
RANK_MATH_DESCRIPTION_KEY = ‘rank_math_description’def update_rank_math_metadata(post_id, title, description):
# Build the URL for the WordPress REST API endpoint
endpoint = f'{WORDPRESS_URL}/wp-json/wp/v2/posts/{post_id}’# Define the metadata to update
metadata = {
RANK_MATH_TITLE_KEY: title,
RANK_MATH_DESCRIPTION_KEY: description
}# Prepare the data to be sent as JSON
data = {‘meta’: metadata}try:
# Send a PATCH request to update the post metadata
response = requests.patch(endpoint, json=data)# Check if the request was successful
if response.status_code == 200:
print(f’Successfully updated Rank Math metadata for post {post_id}’)
else:
print(f’Failed to update Rank Math metadata for post {post_id}. Status code: {response.status_code}’)
except requests.exceptions.RequestException as e:
print(f’An error occurred: {e}’)# Usage example
if __name__ == ‘__main__’:
post_id_to_update = 123 # Replace with the actual post ID
new_title = ‘New Title’
new_description = ‘New Meta Description’
update_rank_math_metadata(post_id_to_update, new_title, new_description)
The ticket ‘Post rankmath title tag and meta description using WP REST API?’ is closed to new replies.