Auto Increment Custom Post Type Title - php

I created a custom post type "Testimonials" and removed support for the title. I am wanting to auto increment the title such as Testimony #1, Testimony #2, Testimony #3, etc.. right now it saves as "auto draft". Any help would be appreciated.
BTW I am not echoing the title, it will only be visible by me.

Made some improvements to the code provided #the-alpha.
// Use a filter to make the change
add_filter( 'wp_insert_post_data' , 'modify_post_title' , '99', 2 );
function modify_post_title( $data , $postarr ) {
// Check for the custom post type and if it's in trash
// We only need to modify if it when it's going to be published
$posts_status = ['publish', 'future', 'draft', 'pending', 'private', 'trash'];
if( $data['post_type'] == 'oferta' && !in_array($data['post_status'], $posts_status)) {
// Count the number of posts to check if the current post is the first one
$count_posts = wp_count_posts('you custom post type');
$published_posts = $count_posts->publish;
// Check if it's the first one
if ($published_posts == 0) {
$data['post_title'] = date('Y-m') . '-1';
} else {
// Get the most recent post
$args = array(
'numberposts' => 1,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'you custom post type',
'post_status' => 'publish'
);
$last_post = wp_get_recent_posts($args);
// Get the title
$last_post_title = $last_post['0']['post_title'];
// Get the title and get the number from it.
// We increment from that number
$number = explode('-', $last_post_title);
$number = $number[2] + 1;
// Save the title.
$data['post_title'] = date('Y-m') . '-' . $number;
}
}
return $data;
}
In this case the user doesn't have the ability to modify the custom post type title and whole thing increments based on the title.

on post submission from frontend custom post title set to #1 but not increment to #2 on second post submission..
This is the exact code used in functions.php
add_filter( 'wp_insert_post_data' , 'modify_post_title' , '99', 2 );
function modify_post_title( $data , $postarr )
{
if( $data['post_type'] == 'your custom post type' ) {
$last_post = wp_get_recent_posts( '1');
$last__post_id = (int)$last_post['0']['ID'];
$data['post_title'] = 'Testimony #' . ($last__post_id+1);
}
return $data;
}

Related

How to save radio button values from html form into advanced custom fields?

I am using WordPress to develop a website and I am hitting a roadblock. How do I save the radio button values from an html form into an advanced custom field? The options that I have show up in the admin but the checked option is not picked. I want to change the color of an object according to the value picked. I am also getting the name and a brief description and that stores in the admin database no problem. I am using wp_insert_post() to submit the form because I want to display the results after submitting.
if(isset($_POST['new_post']) == '1') {
$current_post_author_id = get_the_author_meta( 'ID' );
$post_title = wp_strip_all_tags($_POST['postTitle']);
$post_category = $_POST['review'];
$post_content = wp_strip_all_tags($_POST['postContent']);
$postid = get_the_ID();
$postStarOne = $POST['rateOne'];
$postStarTwo = $POST['rateTwo'];
$postStarThree = $POST['rateThree'];
$postStarFour = $POST['rateFour'];
$postStarFive = $POST['rateFive'];
//$custom_meta = get_post_meta($post->ID, 'new_post', true);
$category='review'; // category name for the post
$cat_ID = get_cat_ID( $category ); // need the id of 'review' category
//If it doesn't exist create new 'review' category
if($cat_ID == 0) {
$cat_name = array('review' => $category);
wp_insert_category($cat_name); // add new category
}
//Get ID of category again incase a new one has been created
$new_cat_ID = get_cat_ID($category);
$new_post = array(
'ID' => '',
'post_author' => $user->ID,
'tax_input' => array($category->taxonomy => array($category->review)),
'post_category' => array($new_cat_ID),
'post_content' => $post_content,
'post_title' => $post_title,
'post_value' => $value,
'post_type' => 'review',
'post_status' => 'pending'
);
$post_id = wp_insert_post($new_post);
$field_key_one = 'group_5fb2ace92f979';
$field_key_two = 'group_5fb2bcfd619dd';
$field_key_three = 'group_5fb2be0da827d';
$field_key_four = 'group_5fb2ace92f979';
$field_key_five = 'group_5fb2bf62167a1';
$value_one = $postStarOne;
$value_two = $postStarTwo;
$value_three = $postStarThree;
$value_four = $postStarFour;
$value_five = $postStarFive;
update_field($field_key_one, $value_one, $post_id);
update_field($field_key_two, $value_two, $post_id);
update_field($field_key_three, $value_three, $post_id);
update_field($field_key_four, $value_four, $post_id);
update_field($field_key_five, $value_five, $post_id);
// This will redirect you to the newly created post
//$post = get_post($post_id);
wp_redirect($post_id->guid);
}
?>
This is the code that I am using to bring in the data from the user. The update field is what I thought was supposed to bring in the data from the post into the custom field but I must be using it the wrong way. I'm not worried about the validation yet. When I get it up and running then I can validate. Any help will be appreciated. Thanks in advance

How can I increment CPT title and slug reliably?

I've got a custom post type called 'tasks' with an ACF front end to post each new one.
I'd like to hide the title from the end user, and have the title & slug automatically set & increment.
Task 1, Task 2, Task 3 etc.
I've tried all the code examples on here I can find & nothing seems to be reliable.
Has anyone got anything they've used in the past that works reliably?
Tried this;
add_filter('title_save_pre','auto_generate_post_title');
function auto_generate_post_title($title) {
global $post;
if (isset($post->ID)) {
if (empty($_POST['post_title']) && 'produktionsauftrag' == get_post_type($post->ID)){
// get the current post ID number
$id = get_the_ID();
// add ID number with order strong
$title = 'produktionsauftrag-'.$id;} }
return $title;
}
Just gives me a post with the title 'untitled' & the next post id as the slug
Tried this one
add_filter( 'wp_insert_post_data' , 'modify_post_title' , '99', 2 );
function modify_post_title( $data , $postarr ) {
// Check for the custom post type and it's status
// We only need to modify it when it's going to be published
$posts_status = ['publish', 'future', 'pending', 'private', 'trash'];
if( $data['post_type'] == 'task' && !in_array($data['post_status'], $posts_status)) {
// Count the number of posts to check if the current post is the first one
$count_posts = wp_count_posts('task');
$published_posts = $count_posts->publish;
// Check if it's the first one
if ($published_posts == 0) {
// Save the title and the slug
$data['post_title'] = date('Y-m') . '-1';
$data['post_name'] = sanitize_title($data['post_title']);
} else {
// Get the most recent post
$args = array(
'numberposts' => 1,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'task',
'post_status' => 'publish'
);
$last_post = wp_get_recent_posts($args);
// Get the title
$last_post_title = $last_post['0']['post_title'];
// Get the title and get the number from it.
// We increment from that number
$number = explode('-', $last_post_title);
$number = intval($number[2]) + 1;
// Save the title and the slug
$data['post_title'] = date('Y-m') . '-' . $number;
$data['post_name'] = sanitize_title($data['post_title']);
}
}
return $data;
}
Does nothing, acf form page just refreshes itself

How to update a category after update a post in WordPress?

I have a custom post type named Beaches. A category is created every time a post is created, this is working.
My category has the same title and the same slug as the post type.
The problem is when a post is updated, I can't link the post to the category anymore, if the title and the slug changes.
This code works to create a category:
function rci_custom_dynamic_beach_category ($post_id) {
$post = get_post($post_id);
if($post->post_status === 'auto-draft') return;
if($post->post_type !== 'beaches') return;
// Verify if the category exists already
$exist = term_exists($post->post_name, 'beaches');
if($exist !== 0 && $exist !== null) {
// THE PROBLEM IS HERE
// Get the category by the slug field
$term = get_term_by('slug', $post->post_name, 'beaches');
wp_update_term($term->term_id, 'beaches', array(
'name' => $post->post_title,
'slug' => $post->post_name
));
}
else {
wp_insert_term($post->post_title, 'beaches', $args = array(
'slug' => $post->post_name,
'parent' => 'houses'
));
}
}
add_action('save_post', 'rci_custom_dynamic_beach_category');
Is there a way to get the old slug value in order to find the term before the post update with the new values?

Custom Fields not showing in custom post type post

I have a custom post type named "Designer" Each posts will be using different unique Advanced Custom Fields as each posts has unique templates.With the below code I am able to give rules for each posts in Designer post type and save but the custom fields are not displaying on post edit pages on backend.
Normally this code should ork but no idea what happend to the code
Please Help.
add_filter('acf/location/rule_types', 'acf_location_rules_types');
function acf_location_rules_types( $choices )
{
$choices['Custom Post types']['cpt_parent'] = 'Custom post type parent';
return $choices;
}
add_filter('acf/location/rule_values/cpt_parent', 'acf_location_rules_values_cpt_parent');
function acf_location_rules_values_cpt_parent( $choices )
{
$args = array(
'hierarchical' => true,
'_builtin' => false
);
$posttypes = get_post_types( $args );
if( $posttypes )
{
foreach( $posttypes as $posttype ):
if( $posttype != 'acf' ):
$args = array(
'post_type' => 'designer',
'posts_per_page' => -1,
'post_status' => 'publish'
);
$customposts = get_posts( $args );
if ( $customposts ) {
foreach( $customposts as $custompost ){
$choices[ $custompost->ID] = $custompost->post_title;
}
}
endif;
endforeach;
}
return $choices;
}
//MATCH THE RULE
add_filter('acf/location/rule_match/cpt_parent', 'acf_location_rules_match_cpt_parent', 10, 3);
function acf_location_rules_match_cpt_parent( $match, $rule, $options )
{
global $post;
$selected_post = (int) $rule['value'];
// post parent
$post_parent = $post->post_parent;
if( $options['page_parent'] ) {
$post_parent = $options['page_parent'];
}
if ($rule['operator'] == "=="){
$match = ( $post_parent == $selected_post );
}
elseif ($rule['operator'] != "!="){
$match = ( $post_parent != $selected_post );
}
return $match;
}
Your Artist Collection field group is set to only appear on one post, the post Designer Post 1 which is a Designer Post type.
I don't understand what all the code is for? Just create a different field group for each post that needs a different field group and a separate rule for each.
Ok sorry I understand the issue now and I have recreated the issue on my local install.
On the line of code below you are looking for the post_parent but I think you should be looking for the ID.
I changed this:
$post_parent = $post->post_parent;
to this:
$post_parent = $post->ID;
and it's working for me.
If I understand your problem correctly, in wp-admin post edit page click on screen options on the upper right corner. In the menu that appears make sure the Custom fields is selected. This will make the custom fields appear for edit.

How to have an empty post title and content when creating a new WordPress post via the front-end

My aim is to insert a new draft post into the database via a custom front-end form I've created. I need the post title and content to be empty. Is this possible?
I've tried the following which doesn't work:
$post_data = array(
'post_title' => '',
'post_type' => 'post',
'post_content' => '',
'post_status' => 'draft',
'post_author' => 1
);
$post_id = wp_insert_post( $post_data );
Note: You can create a new post using the back-end editor with empty title and content so I am wondering how they guys at WordPress do it.
You can not insert a blank post with wp_insert_post, Wordpress will prevent it with wp_insert_post_empty_content hook, you can see it in the source code : https://developer.wordpress.org/reference/functions/wp_insert_post/
The only way to do it is to overpass this hook with a custom function
Here is an example (source)
add_filter('pre_post_title', 'wpse28021_mask_empty');
add_filter('pre_post_content', 'wpse28021_mask_empty');
function wpse28021_mask_empty($value)
{
if ( empty($value) ) {
return ' ';
}
return $value;
}
add_filter('wp_insert_post_data', 'wpse28021_unmask_empty');
function wpse28021_unmask_empty($data)
{
if ( ' ' == $data['post_title'] ) {
$data['post_title'] = '';
}
if ( ' ' == $data['post_content'] ) {
$data['post_content'] = '';
}
return $data;
}
There is a filter in WordPress core that can be used to prevent this.
wp_insert_post_empty_content
https://developer.wordpress.org/reference/hooks/wp_insert_post_empty_content/
Which you should be able to use like:
add_filter( 'wp_insert_post_empty_content', '__return_false' );
$post = wp_insert_post( $post_args );

Categories