php 创建替换帖子标题的功能不起作用

wrrgggsh  于 2023-01-29  发布在  PHP
关注(0)|答案(2)|浏览(141)

我尝试在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_title”变量中,但一无所获

wlzqhblo

wlzqhblo1#

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 );
dbf7pr2w

dbf7pr2w2#

我一直在寻找错误的东西-我希望这能帮助使用ACF /高级自定义字段的人。结果ACF字段还没有保存,当save_post运行时- 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 );
    }

相关问题