CPT without title + WPAlchemy = how save slug?

Home Forums WPAlchemy CPT without title + WPAlchemy = how save slug?

This topic contains 0 replies, has 1 voice, and was last updated by  ecclescake 1 year, 4 months ago.

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #2549

    ecclescake
    Participant

    I’m using WPAlchemy for the first time and really liking it. However, something else I’m doing for the first time is setting up a CPT that doesn’t support a Title. This seems to require me to save a slug manually, otherwise the slug will be auto-draft or something like that. I have a meta field I’d like to use, and I’m trying to use the native WPAlchemy save_action to trigger the save, but I keep running into an infinite loop. Here’s my save code. (I know I’ll have to do a better job of sanitizing, but I’m just trying to get it to work at first.)

    <code>

    function svei_set_slug ( $meta, $post_id ){

    if ( ! wp_is_post_revision( $post_id ) ){

    // unhook this function so it doesn’t loop infinitely

    remove_action(‘save_post’, ‘svei_set_slug’);

    //get first and last name meta

    $first = strtolower($meta['first_name']);

    $last = strtolower($meta['last_name']);

    $slug = $first . ‘-’ . $last;

    //update title

    wp_update_post( array( ‘ID’=&gt;$post_id, ‘post_name’=&gt;$slug ) );

    // re-hook this function

    add_action(‘save_post’, ‘svei_set_slug’);

    }

    }

    </code>

    This code is pretty much straight from the Codex, but I guess wp_update_post triggers the save_action so I still get the infinite loop? Is there any way to take advantage of save_action and save a custom slug based on my meta fields?

    • This topic was modified 1 year, 4 months ago by  ecclescake. Reason: Can't get code to format right…sorry!
Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.