I wanted to update all my pages' excerpt with custom excerpt. So I created my own plugin with few lines of code. I dunno why it is not working, This is my code
function update_my_metadata_new(){
$pages = get_pages();
foreach ( $pages as $page ) {
// Run a loop and update every meta data
if(in_category('books')){
$the_post = array(
'ID' => $page->ID,//the ID of the Post
'post_excerpt' => 'Read books',);
wp_update_post( $the_post );
}
}
}
This plugin will loop into all pages in the given category and update the excerpts, when activated. and I have enabled excerpts for pages by adding this code.
add_post_type_support( 'page', 'excerpt' );
to the functions.php
file.
It looks like you are using in_category
outside of the main loop. You need to pass in the ID of the page for in_category
to work:
in_category('books', $page->ID )
function update_my_metadata_new(){
$pages = get_pages();
foreach ( $pages as $page ) {
/* pass the page ID here */
if( in_category( 'books', $page->ID ) ){
$the_post = array(
'ID' => $page->ID,//the ID of the Post
'post_excerpt' => 'Read books',
);
wp_update_post( $the_post );
}
}
}
register_activation_hook( __FILE__, 'update_my_metadata_new');