Leave a Reply

1 comment

  1. Think about what you’re doing here. You’re hooking into the time which WordPress is saving a post. You need to use get_post_meta() to check if the saving post has that required meta key.

    If you get nothing back, or get_post_meta() returns false, then you need to add your post meta for this saving post using add_post_meta().

    If you get something back, check the values of get_post_meta(), Are they appropriate? Do they need to be changed? This is where you determine if you need to call update_post_meta().

    Once you have validated that your saving post has meta data available to use, THEN you should continue with updating your custom database table… And the same rules apply from the post meta, with your custom database table.

    Check for data in your custom database table. Then determine whether your custom database table should be inserted with data or updated with data.

    Based off the code you provided, I have tried to piece together an example for you to use.

    function my_save_post($post_id, $post){
        global $wpdb;
        /* Not doing an auto save. */
        if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){
            return;
        }
        /* Make sure we have the post data. */
        if(!isset($post)){
            return;
        }
        /* Validate the correct post type and make sure it's not a post revision. */
        if($post->post_type == 'post' && $post->post_status == 'publish' && !wp_is_post_revision($post_id)){
    
            /* Fetch the post meta for this post. */
            $meta = get_post_meta($post_id, 'banner_link', true);
            if(!$meta){
                // No meta for this post with the key of 'banner_link'. Add your post meta.
            } else{
                // Meta data with a key of 'banner_link' was found. Update your post meta.  
            }
    
            /* Meta data has either been added or updated. Fetch the data again. */
            $meta = get_post_meta($post_id, 'banner_link', true);
    
            /* Set the counter to 1. */
            $counter = 1;
    
            /* Check if data already exists for this post in the custom database table. */
            $my_data = $wpdb->get_row("SELECT * FROM {$wpdb->prefix}banner_views WHERE postid = {$post_id}");
            if(!empty($my_data)){
                /* Update the custom database table, with data from this post. */
                $wpdb->update( 
                    $wpdb->prefix.'banner_views', 
                    array( 
                        'link' => $meta,
                        'view_count' => $counter,
                    ),
                    array('postid' => $post_id), 
                    array(
                        '%s',
                        '%d'
                    ),
                    array('%d') 
                );
            } else{
                /* Initially insert the data for this post in the custom database table. */
                $wpdb->insert(
                    $wpdb->prefix.'banner_views', 
                    array( 
                        'postid' => $post_id,
                        'link' => $meta,
                        'view_count' => $counter,
                    ),
                    array(
                        '%d',
                        '%s',
                        '%d'
                    )
                );
            }          
        }
    }
    add_action('save_post', 'my_save_post', 10, 2);
    

    It’s not 100% complete, more specifically around the post meta. Hope this helps.

    My personal opinion…

    You should probably just store everything with post meta, unless there’s a legitimate reason to be storing it in a separate database table.

    Note: You also need to do some work with your counter. Right now, you’re just always setting it as 1 no matter what.