When creating a post how do you select the format?
-
17-04-2021 - |
Question
When creating a post programmatically (with wp_insert_post();
), how do you select the format?
Solution
As post_format is a custom taxonomy and the different formats terms, you should be able to use the tax_input
parameter in the new post args array to set the format. Something along these lines,
$new_post_args = array();
// e.g. $format_slug = 'gallery';
$valid_formats = get_post_format_slugs();
if ( 'standard' !== $format_slug && in_array( $format_slug, $valid_formats ) ) {
$new_post_args['tax_input']['post_format'] = 'post-format-' . $format_slug;
}
wp_insert_post( $new_post_args );
OTHER TIPS
$post_id = wp_insert_post( $args );
$tag = 'post-format-test';
$taxonomy = 'post_format';
wp_set_post_terms( $post_id, $tag, $taxonomy );
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange