게시 제목을 대체할 기능을 만드는 중 작동하지 않습니다.
내부에 함수를 생성하려고 합니다.functions.php커스텀 투고 제목을 여러 커스텀필드의 정보로 바꿉니다.투고를 게시하기 전에 이미 작성되었는지, 아니면 신규 투고인지를 평가하고 싶다.신규 투고일 경우 여러 필드에서 정보를 취득하여 이 커스텀 투고 타입의 투고 총수를 카운트하여 제목을 작성한다.기존 투고를 편집하는 것 뿐이라면, 컨텐츠내의 내용을 사용하고 싶다.$_POST['post_title']들판.
function custom_field_value_as_title( $value, $post_id, $field ) {
global $_POST;
// vars
$title = $_POST['post_title'];
$post = get_page_by_title( $title, 'OBJECT', 'mascotas' );
if ( FALSE === get_post_status( $post_id ) ) {
$new_title_ciudad = get_field('ciudad', $post_id);
$new_title_sexo = get_field('sexo', $post_id);
$new_title_especie = get_field('especie' , $post_id);
$registered_year = date("y");
$count_mascotas = wp_count_posts('mascotas');
$next_count = $count_mascotas->publish + 1;
$new_count = sprintf("%04d", $next_count);
$new_title = "$new_title_ciudad"."$new_title_sexo"."$new_title_especie"."$registered_year"."-"."$new_count";
// update post
$my_post = array(
'ID' => $post_id,
'post_title' => $new_title,
'post_name' => $post_id
);
// Update the post into the database
wp_update_post( $my_post );
} else {
// vars
$new_title = $_POST['post_title'];
// update post
$my_post = array(
'ID' => $post_id,
'post_title' => $new_title,
'post_name' => $post_id
);
// Update the post into the database
wp_update_post( $my_post );
}
}
add_filter('acf/update_value', 'custom_field_value_as_title', 10, 3);
이미 작성된 투고에서는 동작하지만 커스텀 투고 제목을 작성하는 기능은 실행되지 않습니다.좋은 의견이라도 있나?잘 부탁드립니다!
KSNO에서 제안하는 기능으로 교체했습니다.
<?php
function title_replace_function( $post_id, $post ) {
if ( $post->post_date == $post->post_modified ) {
global $_POST;
$new_title_ciudad = get_field('ciudad', $post_id);
$new_title_sexo = get_field('sexo', $post_id);
$new_title_especie = get_field('especie' , $post_id);
$registered_year = date("y");
$count_mascotas = wp_count_posts('mascotas');
$next_count = $count_mascotas->publish + 1;
$new_count = sprintf("%04d", $next_count);
$new_title = "$new_title_ciudad"."$new_title_sexo"."$new_title_especie"."$registered_year"."-"."$new_count";
// update post
$my_post = array(
'ID' => $post_id,
'post_title' => $new_title,
'post_name' => $post_id
);
// Update the post into the database
wp_update_post( $my_post );
} else {
// vars
$new_title = $_POST['post_title'];
// update post
// http://codex.wordpress.org/Function_Reference/wp_update_post
$my_post = array(
'ID' => $post_id,
'post_title' => $new_title,
'post_name' => $post_id
);
// Update the post into the database
wp_update_post( $my_post );
}
}
add_action( 'publish_post', 'title_replace_function', 10, 2 );
?>
투고가 「NEW」가 아닌 경우는, 올바르게 동작합니다.그러나 새 게시물의 새 제목을 만들기 위해 사용자 지정 필드 값을 가져오지 않습니다.제목 필드가 비어 있습니다.$new_title' 변수에 커스텀 필드 값을 추가하려고 했지만 아무것도 추가하지 않았습니다.
if ( get_post_status( $post_id ) === FALSE ) {
wp_update_post( $my_post );
}
절대 있을 수 없는 일이야.false가 반환되는 경우 게시물이 존재하지 않음을 의미합니다.존재하지 않는 투고는 갱신할 수 없습니다.함수의 소스 코드를 확인합니다.
제 생각에 당신의 일을 완수하는 데는 몇 가지 방법이 있을 것 같은데, 그 중 하나를 제안해 드리겠습니다.
function title_replace_function( $post_id, $post ) {
if ( $post->post_date == $post->post_modified ) {
// code for new post
} else {
// code for edited post
}
}
add_action( 'publish_post', 'title_replace_function', 10, 2 );
편집
좋아, 생각보다 쉽지 않았어.다음은 무한 루프(루프 예 1 및 예 2)로부터 보호되며 사용자의 요구에 적합한 테스트입니다.
function title_replace_function( $post_id, $post ) {
if ( ! wp_is_post_revision( $post_id ) ) {
remove_action('save_post', 'title_replace_function');
if ( $post->post_date == $post->post_modified ) {
global $_POST;
$new_title_ciudad = get_field('ciudad', $post_id);
$new_title_sexo = get_field('sexo', $post_id);
$new_title_especie = get_field('especie' , $post_id);
$registered_year = date("y");
$count_mascotas = wp_count_posts('mascotas');
$next_count = $count_mascotas->publish + 1;
$new_count = sprintf("%04d", $next_count);
$new_title = "$new_title_ciudad"."$new_title_sexo"."$new_title_especie"."$registered_year"."-"."$new_count";
$my_post = array(
'ID' => $post_id,
'post_title' => $new_title,
'post_name' => $post_id
);
wp_update_post( $my_post );
} else {
$new_title = 'new_title';
$my_post = array(
'ID' => $post_id,
'post_title' => $new_title,
'post_name' => $post_id
);
wp_update_post( $my_post );
}
add_action('save_post', 'my_function');
}
}
add_action( 'save_post', 'title_replace_function', 10, 3 );
잘못된 것을 찾고 있었습니다.ACF/Advanced Custom Fields를 사용하는 사용자에게 도움이 되었으면 합니다.ACF 필드가 아직 저장되지 않았습니다.save_postruns - ACF가 도움을 줍니다.acf/save_post이 훅을 사용하면 ACF 필드를 사용할 수 있으며 새로운 제목(이 경우 날짜)을 설정할 수 있습니다.
처음 저장 시 및 수정 시 업데이트되므로 날짜/제목이 변경되더라도 항상 일치합니다.
add_action( 'acf/save_post', [ $this, 'set_post_title' ], 10, 3 );
function set_post_title( $post_id ) {
$post = get_post( $post_id );
/* @var $parent_post_id integer */
$parent_post_id = $post->post_parent;
if ( ! $parent_post_id ) {
$parent_post_id = $post_id;
}
/* @var $post_type string */
$post_type = get_post_type( $parent_post_id );
if ( 'post' !== $post_type ) {
return;
}
$fields = get_fields( $parent_post_id );
$date = $fields['date'] ?? 'DATE MISSING';
$new_title = $date . ' (' . $post_id . ')';
$post_update = array(
'ID' => $parent_post_id,
'post_title' => $new_title
);
wp_update_post( $post_update );
}
언급URL : https://stackoverflow.com/questions/38320124/creating-function-to-replace-post-title-not-working
'codememo' 카테고리의 다른 글
| wordpress에서 htaccess가 있는 하위 폴더로 하위 도메인 라우팅 (0) | 2023.03.29 |
|---|---|
| javascript에서 c#(컨트롤러)로 날짜/시간을 전달합니다. (0) | 2023.03.29 |
| angularjs에서 HTML5 지올로케이션을 사용하는 방법 (0) | 2023.03.29 |
| Sublime Text 3에서 TypeScript를 지원하는 방법 (0) | 2023.03.29 |
| 폼데이터를 사용한 JavaScript Blob 업로드 (0) | 2023.03.19 |