Declare $my_post_id
as global in your code. Declare $my_post_id
as global in the-post.php
template. Your code:
global $my_post_id;
$my_post = array(
'post_type' => 'theposttt',
'post_title' => 'test',
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1
);
$my_post_id = wp_insert_post($my_post);
if(is_wp_error($my_post_id))
$my_post_id = 0;
Now, you can use $my_post_id
in the-post.php
template. Possible values: 0 ( failure ), post id ( success ).
Related Posts:
- How to update custom fields using the wp_insert_post() function?
- Disable External Pingacks on WordPress Posts and Only Allow ‘Self Pings’
- New Plugin Review
- Use template for posts with a particular category grandparent
- Query all posts of a custom taxonomy term
- Can’t get wp_insert_post to work
- Get post id in a function when edit/add a post
- wp_trim_words() does not work with my code Am I doing any mistake in my code?
- Which PHP page is the Default Posts Page
- Function works everywhere, how to keep it to execute when creating a new post
- Pagination on Custom Post
- How to use mysql LIKE with wpdb?
- Reading settings in the home page precisely home.php
- Add new post in existing categories using wp_insert_post
- remove specificly the last tag in all posts
- How to Send Pingbacks for all Posts in WordPress?
- posts_per_page displays only 2 posts instead of 4 posts
- Send POST request to Flask app from functions.php file
- How to center all text body in single.php at once?
- Getting page / post URL on publish and / or update
- Displaying POST content with HTML tags and all
- Using Tag Groups: Displaying groups and adjacent tags of current post
- How do I get the featured post to share on Social Media and show on Individual Post pages?
- insert thumbnail image from php script
- Can’t put a hyperlink on Featured Post’s Image
- Weird Behaviour: Not all WordPress Posts appearing
- Published custom posts missing
- WordPress get_post_meta issue
- Insert wordpress tags below posts via functions.php
- Trying to see if page is category or single and displaying title with appropriate heading tag
- How to modify this function to exclude also the post belonging to a specific category?
- How to display the date under the post title?
- Create and publish a new post every Monday, listing the post titles and links to the posts published during the previous week
- Add multiple HTML attributes to an Elementor button
- WooCommerce: write featured image dimensions to custom fields in product’
- How do I get a function to work in single.php
- transition_post_status hook, works – but not if the post is new
- How to change the case of all post titles to “Title Case”
- Cleaning up WordPress to improve performance?
- How can I modify the permalink via a filter?
- wp_set_object_terms and arrays
- The best way to customize “nav-menu-template.php” to add if the ‘link_before’ is “checkbox”
- Output a WooCommerce product custom field in WooCommerce using get_post_meta()
- Can’t load WP function into external function
- Displaying a button on each post
- Image as Sales Badge
- Getting a specific post values to another div or modal
- How to Arrange PHP Files to Allow for Multiple Types of Pages?
- Can I prevent the user from adding more than two levels deep of terms inside of a taxonomy metabox?
- how do I get a specific post from a post with a subcategory in WP
- Retrieve $_POST data submitted from external URL in WordPress(NOT API)
- Css loads very slow [closed]
- how to test for all children (including multilevel grand-childern) of page
- Dilemma of Populating all the categories in a drop down list
- Add item to top of menu using a filter in functions.php
- Limit Taxonomy Output in Conditional Statement
- wp_redirect only works on main site and not on other sites
- How to modify the default feed via a function
- Automatically add images to a menu
- require_once() if a product in woocommerce contains a tag [closed]
- Store metakey value as an array
- How do I change this function from two returns to one string to show sku and dimensions in WooCommerce cart?
- Only show first image in foreach loop
- Shortcode for Listing Users from Meta Value?
- how to enqueque code in a php file from functions.php
- Only load certain artists on this page
- Convert all uploaded PNG files to PNG-8 format
- Uses for function: wp_update_user
- Wp Query : Order by distance lat,lon
- How to Redirect Buddypress Directories To Login Page For Non Logged in Users
- Woocommerce order status payment
- How to define a rule in functions.php that ONLY disables the default wp functionality that undesirably changes ‘&’ to ‘#038;’?
- WordPress Post Block Element not properly parsed with the_content filter
- Display logged in user name and lastname on page
- How to Add a cutsom slug to my custom author role
- How can I check if a post with a particular slug or title exists?
- WordPress sorting posts by date and title using a dropdown
- How to output the taxonomies that are assigned to each custom post?
- Exclude category from function – PHP
- Enqueue script only on child pages of custom post types
- Attach and retrive multiple pdf files to post or page
- Display current user metadata on WordPress page
- When working with a post, almost all wp_postmeta are deleted
- Populate Posts based on category selected using AJAX that means without page refresh
- Why transition_post_type hook is called twice for the same post?
- Can’t get_users info by using json_encode
- What is wrong in this code?
- Add div after every 4 posts then every 2 posts for a responsive loop
- Page returning ID from array, how to return the correct values for post in acf wordpress
- Modify php code to pass a page id as a parameter in order to create a breadcrumb
- Permission error on plugin save
- Removing all my hardcoded URLs with get_site_url()
- WordPress 2014 Theme – Featured Content with multiple tags?
- Highlight main menu when on specific posts or pages
- Insert new post for each user infinite loop error
- Adding Gravity Form With if(is_page) Is Not Working
- Woocommerce – Shipping tax class based on cart items not using the highest tax available
- Hide category by tag
- add_rewrite_rule image from /images/site2/favicon.ico to /favico.ico
- Background Tasks in a WP Cronjob?