From the documentation for post_exists()
:
Return
(int) Post ID if post exists, 0 otherwise.
So you can get the ID just by assigning the result of post_exists()
to a variable:
$post_id = post_exists( $class_post_title );
The same thing works for wp_insert_post()
:
Return
(int|WP_Error) The post ID on success. The value 0 or WP_Error on failure.
So you can do this:
$post_id = post_exists( $class_post_title );
if ( ! $post_id ) {
$post_id = wp_insert_post( $class_post );
}
// $post_id is now the ID of whichever post exists or was created.
Related Posts:
- Loading External Scripts in Admin but ONLY for a Specific Post Type?
- Set posts of a custom post type to be private by default?
- register_post_type sort order by title by default
- Custom post types – Use post_id in permalink structure
- Rewrite custom post type rss feed links
- Populate Taxonomy from Custom Posts
- Order users by number of posts
- Show related posts on single page by custom taxonomy on custom post
- Using previous_post_link and next_post_link to wrap around post sequence
- Same base slug for CPT post and CPT taxonomy terms
- how to make custom posts sticky? [duplicate]
- Custom Post Type, Two categories, Two columns with Pagination
- Replacing the title in admin list table
- Get URL for specific post type and current tag
- How to set custom post type as post title to avoid ‘Auto Draft’
- Parent page for CPT
- Unable to get wp_redirect() working after adding a CPT via the front end
- Post Link Filter Help [closed]
- How to validate custom fields for a custom post type before insert?
- Custom Post Type slug same as page name causing a conflict
- Custom Post Type “Event” with custom permalink structure
- Prevent authors from creating new posts of specific custom-post-type
- Why is my custom loop not filtering correctly nor paginating?
- Display types of posts in edit.php instead of All, Published, Scheduled and Draft
- Custom post types with custom directories
- How to associate two different post types (think Book and Chapter)
- How can I get the first 50% of all posts (within a custom post type)?
- Separate comment section for post type in dashboard
- GravityForm: Populate Dropdown with custom post type [closed]
- Does WordPress limit the length of slug names for Post Meta or Terms?
- Subpages for Custom Post Type. Does that even exist?
- Role capability delete multiple post type posts doesn’t work
- Loop to fetch 3 post_thumbnail instances from 3 most recent custom post types named “portfolio”
- filter custom post type by meta key in dashboard
- How to use load more custom post type data normal or according to on click category tab In WP
- ACF Field on CPT Slug
- Is it possible to create custom post type inside another CPT?
- I need to add endpoint for wordpress categories
- Allow duplicate slugs for custom post type with taxonomies
- Broken featured images are uploading from frontend
- Meta Query Not Returning Output Despite Having Matching Values
- Using taxonomy term in CPT permalink – Pages 404’s
- Custom Post Type Archive URL is wrong
- How to use custom template files in deeper directory?
- Display Custom Taxonomy Alphabetically
- Custom post type archive pagination 404
- Use “archive” as slug for custom post type
- Taxonomy in URL
- Warning: Invalid argument supplied for foreach() [closed]
- Custom post type isn’t working
- Add additional field to custom post_type
- Is it possible to filter the display name for post formats for display in the Formats meta box?
- Creating my own portfolio custom-type
- Multiple pages per post (not pagination)
- Author posts not appearing for custom post type
- How to concatenate two separate colums into one?
- Why content coming from single?
- Strip Characters From Tag Name
- What would be a proper name for a custom post type created for static content?
- Unable to edit categories in custom post type
- Get taxonomy term by slug in post on archive page
- Displaying custom fields for custom post type generate with Custom Post Type UI Plugin
- Query not work for current taxonomy
- Print all the tags from a custom post type
- How to organize custom post type list by year?
- How to add and display custom content that is not a post
- does wp_insert_term link the term to a certain post ID?
- Ideas to connect external database table to wordpress posts as custom field source
- CPT structure questions
- Custom post type category permalinks and archive pages
- All in one calendar custom post type query doesnt work as expected
- Navigate by Tags inside a custom Post Type
- add_filter get array data before display in custom post_type
- WordPress Custom Post Loop
- programmatically generated custom post type is created more than once
- How to show posts of the same category on a page?
- Button link display in shortcodes using custom field in ACF
- Peepso: Add extra data field (custom post meta) to postbox
- Create posts inside CPT post
- wp delete duplicate entries of custom post types every 15 minutes
- Can I manage current & past editions of an annual event with a single WordPress install?
- How to hide sub fields in a field group and also the entire field group if the answer selected is “no”
- Taxonomy to Post Type. Every Taxonomy Item can be Producer or Developer
- WordPress Front-End user restricted taxonomies
- Template hierarchy changes when custom post-type permalink leverages custom taxonomy
- do action inside a class is not working
- Custom Post ‘Post Per Page’ Crashing Page?
- Create an additional template page for every term taxonomy
- Custom Post Type Not Displaying Correctly
- How to solve performance issue caused by custom post type overview with 20k+ custom taxonomy terms
- Query Distinct Taxonomies of Custom Post Type
- Excerpt for each post type and for is_front_page
- Show taxonomies from specific CPT
- Change the text on the Publish button
- strange conditional tag behaviour [duplicate]
- Comments are not working on Custom Post Type
- Change post structure
- How to display posts from IMPress for IDX Broker on home page? [closed]
- Permalinks for Custom Post Types and Taxonomies
- Group search results by post type, but having a unique heading for each section?