I suppose you need somewhere to globally store last order number. Preferable in table wp_options. Method update_option
will help you.
Then you need to make a function which will fires everytime when new order appears.
something like that:
add_action( 'save_post', 'setOrderNumber');
function setOrderNumber($post_id) {
$slug = 'orders'; // slug of your post_type called "Orders"
if ( $slug != $_POST['post_type'] ) {
return;
}
if ( !current_user_can( 'edit_post', $post_id ) ) {
return;
}
$counter = get_option( 'lastOrderCount' ); // example 0051
$counter++; // returns 0052
update_post_meta($post_id, 'purchase_order', 'POCOS'.date('Y').$counter);
update_option( 'lastOrderCount', $counter ); // set the new number of order.
}
I didn’t checked this function, but I hope it works.
Related Posts:
- WP_Query – Order results by meta value
- Query to sort a list by meta key first (if it exists), and show remaining posts without meta key ordered by title
- Filter WP_Query for posts having a certain meta-value
- Can I query custom meta data through WP_Query
- Meta Query with AND & OR?
- WP doesn’t show Array Custom Fields?
- Add new “Insert Into Post” button with another function
- How do I search an array stored in a custom-field using WP_Query?
- Add meta data to the menu
- How do I order by multiple custom fields using wp_query?
- Trying to perform complex custom field query with order by set to field value
- Query meta field using between
- Order Posts by Closest Numeric Values
- 2 orderby in wp_query with 2 custom fields
- Can serialized arrays in DB be matched against serialized arrays with meta_query?
- can you set a default value to a custom field
- Guest Author – How can I use custom fields to create guest author link?
- How to get a meta value from all post
- post meta data clearing on autosave
- Compare meta_query decimals not working right
- WP_Query multiple use of relation and/or
- Query Problem – Show posts within category ‘x’ that have a custom field between ‘y’ and ‘z’
- Add input field to ‘Pages > Edit Page’ through functions.php
- Using OR in WP_Query negates the “NOT EXISTS” compare
- WP_query : meta_key with custom rule for specific value
- How can I combine meta_query queries?
- How to filter a dd/mm/yyyy date from a custom field in a query
- Auto-add paragraphs to custom field?
- Add description text under input field for new profile fields
- How to add a predefined custom field without using a plugin?
- Using WP Query to search by multiple meta fields
- Difference between ‘LIKE’ and ‘IN’ in meta queries
- Compare WP Custom Field date
- Sort by posts that have a featured image?
- WP_Query display next custom post from today’s date
- Order by value in serialized custom field
- Populate Custom Field Dropdown on Theme Install?
- $wp_query meta_key naming issue [closed]
- Author Page Custom Query WHERE author OR [post meta value] OR [post meta value]
- Meta_query and numeric comparison [closed]
- Adding Custom Fields to Search
- How to add content at the end of posts?
- Upcoming Event: How do I sort database by custom date field, but ignore past dates?
- Any way to include custom fields in WP_Query results?
- Multiple relationships in a query
- WP Query Returning All Posts
- How do I use wp_query for WordPress search?
- Meta query with timestamp using WP_query
- Conditional custom field query
- Order query by meta_value with multiple custom fields
- how to put a custom field value in variable
- Custom Metabox Not Saving
- Getting the ID of any image for use in functions.php
- Auto-remove custom field with no value on publish
- Display custom field values from posts on frontpage
- Custom WP_Query for WordPress Search Results with meta_query
- Meta Query And/Or
- How to combine custom fields to make one order-able value
- Redirect to another page using contact form 7? [closed]
- Specific coditional usage [closed]
- Passing meta_box string to post__in?
- Query post by date (stored custom field meta as yyyymmdd) and differentiate across 12 months
- meta_value and meta_key filtering returning no posts [closed]
- Select custom posts by meta_value and sort by a different meta value
- Hide custom fields when empty
- Can’t query by meta_key
- Adding custom field and querying from post table
- How to disable Edit Post and Allow only Custom Field?
- WP Cli – post add meta in xargs after wp post generate – add multiple fields
- No result update a custom field using wp_schedule_event
- Meta query relation ‘OR’ not working as expected
- The best way to collision check in WP
- WP_query multiple custom fields not working
- Custom metabox not working
- Automatic linking terms
- Custom Query Fields – Altering Meta Value
- How to query for posts with either one or another custom field
- Multiple Custom Field Query
- Stripping and/or altering the content of a custom field (video URL)
- How to update custom field of a posts in a particular category
- How can I modify my meta_query to work with prices that are stored in the database that contain dollar signs and commas?
- Odd functions.php issue in WordPress
- How to use custom fields like Title to show value in content?
- Custom Form / Search with Custom Post Type Data
- Query to sort a list by meta key first (if it exists), and show remaining posts without meta key ordered by title
- WP Query multiple select form – meta_query help
- How to Order Posts by Custom Fields?
- WP_Query based on multiple metadata comparisons
- Complex WP_Query Using Post Date And Post Meta
- Display posts where date field matches current month?
- Show posts containing or not custom field
- Meta boxes not displayed, data isn’t being saved
- Calling Custom Field within Function wrapped with Divs.
- Get fields from metabox array
- Including inline Custom Fields info with add_filter in functions.php
- Display agents (custom post type) alphabetically, except one who always shows last
- Query posts by Custom Meta (checkbox) & Genesis Grid Loop
- How to automatically create a custom field when a post is published?
- Custom field in a shortcode?
- Exclude empty fields from custom field calculation (Average)