Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to insert metakey value while saving or updating post but somehow it is not inserting metavalue to link column of my custom talbe called wp_banner_views

function get_publishing_id($post_id) {
    global $wpdb;
    global $post;

    $post= get_post($post_id);
    if ($post->post_type == 'post'
      && $post->post_status == 'publish') {

        $meta = get_post_meta($post->ID, 'banner_link', true);

        if(empty ($counter)) {

            $counter = 1;

            $wpdb->insert( 
                $wpdb->prefix . 'banner_views', 
                array( 
                    'postid' => $post->ID,
                    'link' => $meta,
                    'view_count' => $counter,
                ),
                array(
                    '%d',
                    '%s',
                    '%d'
                )
            );          
        }



    }
}
add_action('save_post','get_publishing_id'); 

I am trying to make click and view counter for banner. I have done view counter but trying to create click counter. Any autosuggestion for that would be appropriated.

share|improve this question
add comment

1 Answer 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.

share|improve this answer
    
I can see you really put your great effort to help me. I really appreciate it. I will check this and get back to you. For counter I have another function which is updating on refresh. However learn me or give me some hint if this is wrong approach for it. –  pixelngrain Oct 4 '12 at 18:46
    
@pixelngrain It is always a good idea to have separate functions for specific tasks. It makes your code more maintainable and adaptable. Read the Law of Demeter on Wikipedia. As for your counter, you could create two different functions. One for logging the page views (incrementing every time the post is viewed) and another for unique clicks, based upon the visitor's IP address (incrementing only once per IP address). You could store both of these bits as separate post meta keys for the viewed post. –  Michael Ecklund Oct 4 '12 at 20:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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