• Hi all.
    I wonder how to do this. I’ve made a custom post type called swimmers with a meta box containing some values of interest and everything is ok. I can insert and save data with no problem.
    Now I’ve got a custom table in the database for the swimming competitions and I wonder if this could pull some of the data from the swimmer post type and from that custom meta box in order to prevent writing the same values over and over again.

    In the specific, I’d like to get the swimmer post title (aka the swimmer name) and copy it into the custom table. When can I do it? While I’m saving the meta box or while I’m publishing the post? I think both are possible, but I’m not able to insert the value into the custom table.

    This is my code (not working):

    function update_table_with_swimmers(){
    
    global $wpdb;
    $table_name = $wpdb->prefix . 'swim';
    $title = get_the_title();
    $values = array(
      'athlete' => $title,
    );
    $formats_values = array(
      '%s',
    );
    $wpdb->insert( $table_name, $values, $formats_values );
    }
    
    add_filter( 'publish_post', 'update_table_with_swimmers' );

    [Moderator Note: Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum’s parser.]

    What about retrieving values from meta box and copying them into that custom table?

    Thank you in advance

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    I think you only problem is using get_the_title() without any parameters. It can only be used inside the Loop like that. With the ‘publish_post’ filter, you can have the post object passed as a second parameter, you can get the title directly from that. You need to alter how you add the filter and define the callback.

    add_filter( 'publish_post', 'update_table_with_swimmers', 10, 2 );
    function update_table_with_swimmers($post_id, $post){
    $title = $post->post_title;

    Another option, which I believe is not as efficient but closer to what you already have is this:

    add_filter( 'publish_post', 'update_table_with_swimmers');
    function update_table_with_swimmers($post_id){
    $title = get_the_title($post_id);

    Of course, I’m only showing you the parts that need to be altered, you need to include all the other code as well.

    Thread Starter 2olive

    (@2olive)

    Thanks bcworkz. I tried with no success. I’m writing another piece of code that might be the right one.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Custom post type and meta box into custom table’ is closed to new replies.