user3324136
user3324136

Reputation: 417

Wordpress how to get advanced custom field content using python and the REST API

I am using a REST API with Python to get custom field content from WordPress. When I pull a non custom field like "Title", I can successfully render this with the code:

post_title = post_data_dict.get('title', {}).get('rendered', '')

However, with a custom field, I haven't been able to figure this out. Right now my code looks like this:

video_content = post_data_dict.get('acf', {}).get('content', '')

However this contains the HTML formatting.

Any hints would be greatly appreciated. Thank you.

Upvotes: 0

Views: 356

Answers (1)

user3324136
user3324136

Reputation: 417

I found this post that worked for me: Python code to remove HTML tags from a string

I just used the function:

def cleanhtml(raw_html):
    cleanr = re.compile('<.*?>')
    cleantext = re.sub(cleanr, '', raw_html)
    return cleantext

So, my new code looked like this:

video_content = cleanhtml(post_data_dict.get('acf', {}).get('content', ''))

Upvotes: 0

Related Questions