[Plugin auto-create categories] get_the_title returning 'Auto Draft' (4 posts)

  1. Smaug56
    Posted 3 years ago #

    Hi all,

    I'm building a plugin to create a new category every time a new page is created, and naming each one 'Sidebar: $page_title'. For this, I am hanging a function from save_post:

    In plugin .php:
    add_action('save_post', 'create_sidebar_cat');

    In functions.php

    function create_sidebar_cat($post_id)
             if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
             $pcheck = $_GET["post_type"];
             if($pcheck != "page") return;
             if ( !wp_is_post_revision( $post_id ) )
                $post_title = get_the_title( $post_id );
                $cname = 'Sidebar: '.$post_title;
                wp_create_category ($cname, 3);}

    It is working to a certain extent - the category is being created, and with the correct parent (id 3). But it is being named 'Sidebar: Auto Draft'. Does anyone know why this is happening, or can suggest a better way of calling the new page title when 'publish' is clicked?

    Many thanks in advance,


  2. Smaug56
    Posted 3 years ago #

    moving this to the sub-forum, as it seems more appropriate there

    [mod: redundant duplicate removed]

  3. netcs.vg
    Posted 3 years ago #

    im new here, but sounds like the post hasn't been saved/published yet. Why not get the title from the $_GET array?

  4. Smaug56
    Posted 3 years ago #

    Thanks for the suggestion netcs. I just gave that a go, but $_GET(["post_title"]; is empty. The autodraft message is gone as well now.

    The somewhat clumsy way I tested this was by adding this code to send an email to myself:

    $post_title = $_GET["post_title"];
    $cname = 'Sidebar: '.$post_title;
    $catmessage = "The GET title is ".$GET["post_title"]." and the variable is ".$post_title.". The cat name is ".$cname;
    mail("myemail@mail.com","New category created",$catmessage);

    Any other ideas?


Topic Closed

This topic has been closed to new replies.

About this Topic