Although the name of the function wp_publish_post()
suggests that it can be used to publish a post it should obviously not be used to publish a draft post programmatically.
The way to do so is to use wp_update_post()
and set the post status manually to publish
:
<?php
// Update post with the ID 42
$postData = [ 'ID' => 42, 'post_status' => 'publish' ];
wp_update_post( $postData );
This will create a valid value for the post_name
field and thus a proper permalink.
The function wp_publish_post()
is used by WordPress only for publishing future posts during a scheduled hook (see check_and_publish_future_post()
). The reason why this won’t break permalinks is, that posts with status future
already have a valid value for post_name
created by wp_insert_post()
.
Related Posts:
- Permalinks: custom post type -> custom taxonomy -> post
- Mixing custom post type and taxonomy rewrite structures?
- Resolve a custom post type name vs. page permalink conflict (same slug)
- How to rewrite URI of custom post type?
- How to change permalink structure for custom post type and it’s taxonomies?
- Nested custom post types with permalinks
- Change permalinks for posts but not for custom post types
- Custom post types – Use post_id in permalink structure
- Custom post type 404s with rewriting even after resetting permalinks
- Custom post type permalink: only use %post_id% and remove %postname%
- Permalink rewrite 404 conflict- WordPress Taxonomies/ CPT
- Rewrite custom post type rss feed links
- Numeric slug on child post
- 404 on child page when parent page slug is identical to custom post type
- rewrite get parameter with custom post type
- Remove unwanted part of permalink custom structure from CPT url?
- Custom permalinks – post type – hierarchical taxonomy’s
- Changing a custom post type “has_archive” after registered
- Remove Slug from Custom Post Type results in 404
- dynamically update permalink and title with the values of custom fileds
- How does one flush rules on theme activation or deactivation only?
- Change permalink structure for custom post type
- How to create a permalink for custom taxonomies and custom post types like base-name/parent-tax/child-tax/custom-post-type-name
- How to show custom taxonomy in the permalink?
- Date-based permalinks for Custom Post Type, and custom taxonomy permalinks
- WordPress rewrite my URL when i use pagination
- Permalinks for custom post-types within custom taxonomies not found
- Registration and Custom Post Types – How to synchronize information?
- Passing parameters to WordPress static front page
- After updating a page, all pages on the site used the index.php template
- Parent & Child Taxonomy slugs in custom post permalink
- Custom Post Types and independent Categories – complex Taxonomy
- Easy way to change custom post type name for permalinks?
- Stop / prevent WordPress from updating permalinks of custom post type
- Use menus with anchors
- Permalink structure by Post Meta value
- custom post type parsed as attachment
- 404 Issue w/ Custom Post Type – using Meta for Permalink rewrite
- WordPress Custom Post Type Permalinks dynamic term slugs
- How to add rewrite rule to custom post type with parent in url with cpt ui?
- Create a custom php page and load it at a specific slug
- Permalinks /country/city/cpt/postname/
- Seamless permalinks between Custom Taxonomy and Custom Posts
- Custom post type and custom taxonomy with the same slug using the file page taxonomy only as directory
- Permalinks for custom post type
- Path to custom post type
- How to change permalink to include custom post type
- Make custom post types and pages place nice, re: rewrite
- set permalink for CPT
- One Custom post type 404’s others don’t
- Permalink problems with custom post type and custom taxonomy
- Route subpage to Custom Post Type
- Custom Post Type “MUST NOT” be able to search via URL
- Custom Post Type Advanced Slug
- Ideas on how to organize a project [closed]
- Under What Conditions Can I name my CPT the same as my CPT Page?
- Custom post type category permalinks and archive pages
- One Custom Post Type two different Templates
- Adding /blog in front of single posts (only)
- How to remove /blog/ prefix for custom post types in permalinks?
- On saving Post My permalink is updated and category is removed automatically which cause the 404
- Searching in multiple category + URL hierarchy in real estate WordPress site
- Custom post ID & display information related to this ID
- Is there a way to use ‘publicly_queryable’ => false only on specific taxonomy term?
- Edit the default ‘post’ custom post type arguments
- add all blog posts to folder
- Category permalinks don’t work even after flushing
- Changing permalink of default Post with custom post
- Custom Taxonomy Archive generates 404
- Enabling hierarchical in a Custom Post Type gives error 404
- Standard Page Child of CPT Not Found (404)
- How to create a permalink structure with custom taxonomies and custom post types
- Pagination not working (page/2 retunring 404 page)
- Permalinks change when edit published post on custom post type
- Custom permalink rewrite rules – how do I fix this?
- Template hierarchy changes when custom post-type permalink leverages custom taxonomy
- How to retrieve custom post type permalink in another cpt?
- Associate custom CPT with another CPT’s taxonomy and rewrite permalinks
- Permalinks for a custom post type – with a page template (custom query) and taxonomy archive
- Post Custom & Taxonomy 404 error
- WordPress custom post type rewrite rule matches all pages returning 404
- Same permalink structure for all post types?
- the_permalink() leads back to homepage
- How to add base permalink to custom posts?
- Custom post type with parent page? Possible?
- Hierarchical taxonomy in permalink structure
- Can’t make permalink of CPT work
- Issue with permalinks for custom post type
- Custom post type using standard post categories results in 404 when viewing /cpt/category
- Cross post type parent page added parent page to permalink but give 404
- Category / Custom Post Type permalink issue
- Adding rewrite rules to Custom Post-type mixed with Taxonomy
- “regular” pages not found after changing permalink
- Custom Post Type and Taxonomy pagination permalink
- permalink structure for single-{post-type}.php in wordpress
- Linking from a list of custom post type items to a single – get_post_permalink not working correctly
- Custom post type permalink structure
- How do I use CPT and permalinks with a mobile theme?
- Add /blog to the “post” post type url
- Can I have a specific template for products even if they are standard posts and not CPT?