• Resolved wzshop

    (@wzshop)


    Hi,
    As you suggested i tried the code below:

    function custom_pvc_import_meta_key( $meta_key ) {
    	// Use your meta-key here
    	$meta_key = 'post_views_count';
    	return $meta_key;
    }
    add_filter( 'pvc_import_meta_key', 'custom_pvc_import_meta_key' );

    ive added it into my functions.php and i have a post metafield called ‘post_views_count’ with the corresponding post views.
    However when clicking the import button (‘Override existing views data’ selected ) no data is imported into the ‘wp_post_views’ table.. What am i doing wrong?

Viewing 1 replies (of 1 total)
  • Thread Starter wzshop

    (@wzshop)

    Ive created a plugin. Not an elegant way but it works. Just create a php file and upload it into your plugins folder and activate the plugin (Deactivate afterwards).

    The way for me to fix it was to set the wpdb->query inside of the foreach statement. Any more elegant ways are welcome, thanks

    /**
    * Plugin Name: Views import for post views count
    */
    function rt_import_post_views(){
    global $wpdb;	
    	$meta_key = 'views';
    		$views = $wpdb->get_results( "SELECT post_id, meta_value FROM " . $wpdb->postmeta . " WHERE meta_key = '" . $meta_key . "'", ARRAY_A, 0 );
    			if ( ! empty( $views ) ) {
    				$sql = array();
    				foreach ( $views as $view ) {
    					$wpdb->query( $wpdb->prepare("INSERT INTO " . $wpdb->prefix . "post_views (id, type, period, count) VALUES ( %d, %d, %s,%d )",
        array(
          $view['post_id'],
    	  4,
    	  'total',
    	  $view['meta_value']
        )
    	));
    				}
    	} // close get action
    }
    add_action( 'init', 'rt_import_post_views' );
    • This reply was modified 8 years, 8 months ago by wzshop.
    • This reply was modified 8 years, 8 months ago by wzshop.
Viewing 1 replies (of 1 total)

The topic ‘Importing from post meta?’ is closed to new replies.