4

I am using Gravity Forms to allow users to create posts from the front end.

However, the way this site I am building is going to work, one specific Post_Type is essentially just a "Data" post. There is a specific Taxonomy who's Terms are actually the "Title" of the data set.

I have already figured out a way to hide the "Title" field in Gravity forms but still have it present on the form (for some reason they lock the "Post_Type" and "Post Status" into the 'Title' field in the GUI, so I overloaded it's conditional logic to force it to always hide).

I want to create a filter that would always copy the [Term] value for a specific [Taxonomy] into the [Title] field of a specific [Post_Type].

Any advice I could get on how to accomplish this would be great! Thanks in advance!

==============================================================

OK, using code from @Manny Fleurmond below, and from this post: Title_save_pre - Simple problem that u know for sure

I came up with

function taxonomy_title_rename($title) {
    global $post;
    $type = get_post_type($post->ID);
    if ($type== 'CUSTOM_POST_TYPE') {
            if($post->post_type === 'CUSTOM_POST_TYPE') {
        $terms = wp_get_object_terms($post->ID, 'MY_TAXONOMY');
        if(!is_wp_error($terms) && !empty($terms)) {
            $title = $terms[0]->name;
        }
        }
    return $title;
}
     else if ($type != 'CUSTOM_POST_TYPE') {
       return $title;
    }
    }
    add_filter ('title_save_pre','taxonomy_title_rename');

This is actually saving the post title to the post from the taxonomy. However, it is not then pulling it into the permalink as well, my permalink is just the post ID number. Will see if I can get this solved on my own, but any help would be appreciated!

1
1

I've pieced together a solution. Let me know if it's what you need:

add_filter('the_title','term_filter',10,2);
function term_filter($title, $post) {
    $post = get_post($post) ;
    if($post->post_type === 'special_post_type') {
        $terms = wp_get_object_terms($post->ID, 'taxonomy');
        if(!is_wp_error($terms) && !empty($terms)) {
            $title = $terms[0]->name;
        }
    }
    return $title;
}

Basically, I'm using a filter for the title that checks what post type the title is from. If it is the correct type, the title is replaced by the first term of your taxonomy that is connected to that post.

Ask me any questions if something isn't clear!

2
  • Hey man, thanks for taking the time to take a stab at this for me! This is Close, but it seems to just be masking the Title... only in the back-end list for posts does it show the converted title, when one goes to edit the post, the post is still definitely named "Untitled" and the permalink is also "/untitled". But ya definitely did something close to what I'm going for, it just seems to be a mask instead of an actual change. – Sam K Jul 10 '11 at 15:34
  • I updated my top post with a fix I came up with using some of your code, but came up with a new problem as well! Thanks for the help. – Sam K Jul 10 '11 at 22:17
1

Well, since I essentially came up with my own answer using what was posted (which didn't really work) plus some extra research, I am going to close this question, even though I sort of added an additional question;

OK, using code from @Manny Fleurmond below, and from this post: Title_save_pre - Simple problem that u know for sure

I came up with

function taxonomy_title_rename($title) {
    global $post;
    $type = get_post_type($post->ID);
    if ($type== 'CUSTOM_POST_TYPE') {
            if($post->post_type === 'CUSTOM_POST_TYPE') {
        $terms = wp_get_object_terms($post->ID, 'MY_TAXONOMY');
        if(!is_wp_error($terms) && !empty($terms)) {
            $title = $terms[0]->name;
        }
        }
    return $title;
}
     else if ($type != 'CUSTOM_POST_TYPE') {
       return $title;
    }
    }
    add_filter ('title_save_pre','taxonomy_title_rename');

This is actually saving the post title to the post from the taxonomy.

1
  • This is only working when the post is updated not saved. – amarinediary Jan 4 at 19:54
0

Knowledge

The filter title_save_pre runs before the post is written to the database, and also before any of its post meta is saved to the database.

wp_get_object_terms at this point will be blank.

To correct this you'll want to hook into save_post and update the post from there. save_post runs after the post has been saved to the database, and the in-built 'custom fields' have been saved.

There is a few things to understand before using save_post like the infinite loop case. Overall you were pretty close.


Demo

I've commented everything. I've added a bit of jquery to disable the title input and a filter to update the default placeholder. I've also restricted the ability to publish, if there is no taxonomy selected.

<?php
add_action( 'init', function() {

  /**
  * Determines whether the current request is for an administrative interface page
  * @link https://developer.wordpress.org/reference/functions/is_admin/
  */
  if( ! is_admin() ) {
    return;
  };

  global $custom_taxonomies, $custom_post_type;
  $custom_post_type = '_custom_post_type_'; // set your custom post type
  $custom_taxonomies = [ '_first_custom_taxonomy_', '_second_custom_taxonomy_' ]; // set your custom taxonomies

  /**
  * Disable title input
  * @link https://developer.wordpress.org/reference/hooks/enter_title_here/
  */
  add_action( 'admin_head', function() {

    global $custom_post_type, $custom_taxonomies;
    if( get_post_type() !== $custom_post_type ) {
      return;
    };

    echo "<script type='text/javascript'>
    jQuery( document ).ready( function( $ ) {
      $( '#title' ).css( 'cursor', 'not-allowed' ).attr( 'readonly', true ).attr( 'disabled', true );

      var term = $( 'input[name^=\"tax_input\"]:checkbox' );
      term.change( function() {
        $( '#publish, #save-post' ).prop( 'disabled', term.filter( ':checked' ).length < 1 );
      } );
      term.change();
    } ); </script>";
  } );

  /**
  * Replace the default placeholder
  * @link https://developer.wordpress.org/reference/hooks/enter_title_here/
  */
  add_filter( 'enter_title_here', function( $placeholder ) {

    global $custom_post_type, $custom_taxonomies;
    if( get_post_type() !== $custom_post_type ) {
      return;
    };

    $placeholder = "Title automatically generated";
    return $placeholder;
  } );

  /**
  * Fires once a post has been saved
  * @link https://developer.wordpress.org/reference/hooks/save_post/
  */
  add_action( 'save_post', 'automatically_generated_title', 10, 2 );
  function automatically_generated_title( $post_id, $post ) {

    /**
    * Prevent function if custom the post type isn't matching
    * ...or if the post is published as auto-draft
    * @link https://wordpress.stackexchange.com/a/218291/190376
    * @link https://wordpress.stackexchange.com/a/217101/190376
    */
    global $custom_post_type, $custom_taxonomies;
    if ( $post->post_type != $custom_post_type || $post->post_status == 'auto-draft' ) {
      return;
    };

    /**
    * Prevent function on auto save
    * @link https://wordpress.stackexchange.com/a/218291/190376
    */
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
      return;
    };

    /**
    * Prevent title from updating on post update
    * @link https://wordpress.stackexchange.com/a/177670/190376
    */
    $created  = new DateTime( $post->post_date_gmt ); // get post publication date
    $modified = new DateTime( $post->post_modified_gmt ); // get post modification date
    $delta = $created->diff( $modified ); // calculate delta between $created and $modified
    $delay = ( ( ( $delta->y * 365.25 + $delta->m * 30 + $delta->d ) * 24 + $delta->h ) * 60 + $delta->i ) * 60 + $delta->s; // handle the processing when it begins at the end of a given second, the post_date_gmt and the post_modified_gmt might be different

    if( $delay <= 1 ) {

      /**
      * wp_get_object_terms instead of get_the_terms to retrieve an array() of taxonomies
      * @link https://developer.wordpress.org/reference/functions/wp_get_object_terms/
      */
      $terms = join( '', wp_list_pluck( wp_get_object_terms( $post_id, $custom_taxonomies ), 'name' ) );
      $self = str_replace( '-', '', strtoupper( get_post_type() . $terms . $post_id ) ); // set your title structure

      /**
      * Prenvent infinite loop case
      * @link https://developer.wordpress.org/reference/hooks/save_post/#avoiding-infinite-loops
      */
      remove_action( 'save_post', 'automatically_generated_title' ); // unhook this function so it doesn't loop infinitely

      /**
      * Update a post with new post data
      * @link https://developer.wordpress.org/reference/functions/wp_update_post/
      * Parameters based on wp_insert_post @see https://developer.wordpress.org/reference/functions/wp_insert_post/#parameters
      */
      wp_update_post( array(
        'ID' => $post_id,
        'post_title' => $self,
        'post_name' => $self,
      ) ); // update the post, which calls save_post again

      add_action( 'save_post', 'automatically_generated_title', 10, 2 ); // re-hook this function

    };

  };

} ); ?>

The title will set itself upon first-publishing submission. It will then ignore any update made to the post to keep a consistent slug. You can easily override that behaviour by removing the code in-between if( $delay <= 1 ) { ..., moving it outside of the statement, and removing the following lines including the if( $delay <= 1 ) { ... itself:

$created  = new DateTime( $post->post_date_gmt );
$modified = new DateTime( $post->post_modified_gmt );
$delta = $created->diff( $modified );
$delay = ( ( ( $delta->y * 365.25 + $delta->m * 30 + $delta->d ) * 24 + $delta->h ) * 60 + $delta->i ) * 60 + $delta->s;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.