Skip to content

Instantly share code, notes, and snippets.

@wezoalves
Created September 10, 2024 02:58
Show Gist options
  • Save wezoalves/5e2455cf158833ff797d40f01ed4e575 to your computer and use it in GitHub Desktop.
Save wezoalves/5e2455cf158833ff797d40f01ed4e575 to your computer and use it in GitHub Desktop.
Update meta values into post WordPress
import pandas as pd
import requests
import time
from requests.auth import HTTPBasicAuth
def update_post_meta(post_id, meta_key, meta_value, wp_user, wp_password, wp_url):
url = f"{wp_url}/wp-json/ra/v1/update-meta"
data = {
"post_id": post_id,
"meta_key": meta_key,
"meta_value": meta_value
}
auth = HTTPBasicAuth(wp_user, wp_password)
response = requests.post(url, json=data, auth=auth)
if response.status_code == 200:
print(f"Meta field atualizado para o post {post_id}")
else:
print(f"Erro ao atualizar o post {post_id}: {response.status_code}, {response.text}")
def read_csv(file_path):
df = pd.read_csv(file_path)
return df
def main():
wp_url = "https://domain.com/"
wp_user = "user-wordpress"
wp_password = "password-app-wordpress"
meta_key = "key-meta"
meta_value = "value-meta"
file_path = "file-source-ids.csv"
df = read_csv(file_path)
for post_id in df['ID']: # Substitua 'ID' pelo nome da coluna no csv
update_post_meta(post_id, meta_key, meta_value, wp_user, wp_password, wp_url)
time.sleep(3)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment