Last active
March 16, 2022 16:58
-
-
Save kbcarte/ad49aaf6bbb06312a0a31c4906611d4c to your computer and use it in GitHub Desktop.
Example of querying wordpress posts, pages, and custom post types, then update the posts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// this will probably take a while to run | |
// so don't refresh the page, if the browser complains | |
// continue to wait | |
// -1 posts per page will give ALL | |
// will default to 10 if blank | |
// default blog posts | |
$blog_args = array( | |
'post_type' => 'post', | |
'posts_per_page' => -1 | |
); | |
$blog_posts = get_posts($blog_args); | |
// default pages | |
$page_args = array( | |
'post_type' => 'page', | |
'posts_per_page' => -1 | |
); | |
$page_posts = get_posts($page_args); | |
// custom post types | |
// example from thompson creek | |
$cpt_args = array( | |
'post_type' => 'buying-guides', | |
'posts_per_page' => -1 | |
); | |
$cpt_posts = get_posts($cpt_args); | |
// get_posts() returns an array of post objects | |
// obj holds things like title, author, publish date, etc.. | |
foreach($blog_posts as $p){ | |
// do the stripping here, lol | |
// be sure that the obj has the updated content | |
// ex: $p->post_content = $tripped_content; | |
var_dump($p->post_content); | |
// update the post | |
wp_update_post($p); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment