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

Posted

技术标签:

【中文标题】创建替换帖子标题的功能不起作用【英文标题】:Creating function to replace post title not working 【发布时间】:2016-11-14 04:17:20 【问题描述】:

我正在尝试在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”变量中,但什么也没有

【问题讨论】:

检查编辑的答案 【参考方案1】:
if ( get_post_status( $post_id ) === FALSE ) 
    wp_update_post( $my_post );

永远不会发生。如果返回 false,则表示 post 不存在。您无法真正更新不存在的帖子。 Check source code of the function.

我认为完成任务的方法很少,但我只推荐其中一种。

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

编辑

好吧,没我想的那么容易。这是经过测试的,可以防止无限循环(注意循环Example 1 和Example 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 );

【讨论】:

谢谢 ksno!我要写的是,如果返回 FALSE(意味着帖子不存在),则用自定义字段中的内容替换帖子标题。如果返回 TRUE,则只需更新 $_POST['post_title'] 字段的内容 我正在使用您的功能,但它没有生成新标题: 我正在使用您的功能,但它没有生成新标题。当我创建新帖子时,发布帖子后标题字段为空 @GustavoCohen 检查编辑。我认为你的问题应该得到解决。 嗨,ksno!我今天要试试你的功能。非常感谢您花时间编写代码!

以上是关于创建替换帖子标题的功能不起作用的主要内容,如果未能解决你的问题,请参考以下文章

Opsgenie powershell 警报帖子不起作用

使用 python 和 QML Oscilloscope 动态绘图效果很好,但相同的程序在树莓派中不起作用,替换功能不起作用

Wordpress“帖子名称”永久链接不起作用

自定义帖子类型单页不起作用

Facebook REST API Like/UnLike 任何帖子都不起作用

Firebase - 跨多个位置的原子写入不起作用