I'm trying to get the content of a Wordpress post as plain text. Looking in the database is see this in column post_content
<!-- wp:acf/banner-slim {"name":"acf/banner-slim","data":{"subhead":"","_subhead":"field_674cafb053c24"},"mode":"auto"} /-->
foobar
<!-- wp:acf/block-intro-child {"name":"acf/block-intro-child","data":{"intro":"Effective B2B digital marketing is about skilled performance. The stats don’t lie. If a campaign fails, you can’t justify the cost by saying it was ‘good for the brand’.\r\n\r\nSuccessful performance requires a number of factors to come together, each being deep specialisms in their own right. ","_intro":"field_67b3632a5dc58","breakout":"At our B2B digital marketing agency in Bath we work in local, national and international markets. We are strategically led and constantly thinking of ways to make your marketing more effective. ","_breakout":"field_67b363775dc59"},"mode":"auto"} /-->
but when I use various Wordpress functions to try to get the content without filters – get_the_content()
, $post->$post_content
all I get is foobar (I inserted this into the database column myself). The rest, enclosed in HTML comments is stripped out.
How do I load this content into a variable without any stripping?
At the moment any attempt to load the WP_Object into a variable results in this;
WP_Post Object (
[ID] => 8349
[post_author] => 1
[post_date] => 2024-11-29 08:19:01
[post_date_gmt] => 2024-11-29 08:19:01
[post_content] => foobar
[post_title] => Strategic B2B Marketing
[...]
I'm trying to get the content of a Wordpress post as plain text. Looking in the database is see this in column post_content
<!-- wp:acf/banner-slim {"name":"acf/banner-slim","data":{"subhead":"","_subhead":"field_674cafb053c24"},"mode":"auto"} /-->
foobar
<!-- wp:acf/block-intro-child {"name":"acf/block-intro-child","data":{"intro":"Effective B2B digital marketing is about skilled performance. The stats don’t lie. If a campaign fails, you can’t justify the cost by saying it was ‘good for the brand’.\r\n\r\nSuccessful performance requires a number of factors to come together, each being deep specialisms in their own right. ","_intro":"field_67b3632a5dc58","breakout":"At our B2B digital marketing agency in Bath we work in local, national and international markets. We are strategically led and constantly thinking of ways to make your marketing more effective. ","_breakout":"field_67b363775dc59"},"mode":"auto"} /-->
but when I use various Wordpress functions to try to get the content without filters – get_the_content()
, $post->$post_content
all I get is foobar (I inserted this into the database column myself). The rest, enclosed in HTML comments is stripped out.
How do I load this content into a variable without any stripping?
At the moment any attempt to load the WP_Object into a variable results in this;
WP_Post Object (
[ID] => 8349
[post_author] => 1
[post_date] => 2024-11-29 08:19:01
[post_date_gmt] => 2024-11-29 08:19:01
[post_content] => foobar
[post_title] => Strategic B2B Marketing
[...]
Share
Improve this question
asked Feb 17 at 17:26
Chris PinkChris Pink
2,0272 gold badges20 silver badges32 bronze badges
2
|
2 Answers
Reset to default 3whether you use get_the_content()
or get_post_field()
doesn't change the problem, if you want to retrieve the HTML comments used by WordPress, you need to use the htmlentities()
function like this:
$content = get_the_content(null, false, $post_id);
echo htmlentities($content);
Or
$content = get_post_field('post_content', $post_id);
echo htmlentities($content);
If you want the raw HTML you can use
$content = get_post_field('post_content', $post_id);
$post_id = get_the_id(); $sql = "SELECT post_content FROM wp_posts WHERE ID = $post_id"; global $wpdb; $result = $wpdb->get_results($sql); print_r($result);
Strips out the html comment. How does this work? – Chris Pink Commented Feb 17 at 17:38parse_blocks($post->post_content)
- that should get you everything. Also, since that's rendering block code I think wp adds it on the server side, it's not in thepost_content
property of the object. – disinfor Commented Feb 17 at 21:53