Milo是对的,这是个坏主意,但我被迫在一个大网站上这样做,因为在未索引meta\\u值上的关系太慢了。下面是我的示例post_views
柱Use it with caution and only if you are aware of what you are doing!
<?php
define("AMP_OPTION_VIEWS", \'_prefix_views\');
/**
* Pridava funkie okolo pocitania zobrazeni a kliknuti pre posty
*/
function prefix_get_count($post_id = 0)
{
$post = get_post($post_id);
return isset($post->post_views) ? intval($post->post_views) : 0;
}
function prefix_set_count($views, $post_id = 0)
{
global $wpdb;
$post_id = $post_id ?: get_the_ID();
if ($post_id) {
$wpdb->query($wpdb->prepare("UPDATE " . $wpdb->posts . " SET post_views = %d WHERE ID = %d", $views, $post_id));
}
return !$wpdb->last_error;
}
function prefix_increment_count($post_id = 0)
{
global $wpdb;
$post_id = $post_id ?: get_the_ID();
if ($post_id) {
$wpdb->query($wpdb->prepare("UPDATE " . $wpdb->posts . " SET post_views = post_views + 1 WHERE ID = %d",
$post_id));
}
return !$wpdb->last_error;
}
function prefix_posts_columns($defaults)
{
if (current_user_can(\'edit_published_posts\')) {
$defaults[AMP_OPTION_VIEWS] = __("Views");
}
return $defaults;
}
add_filter(\'manage_posts_columns\', __NAMESPACE__ . NAMESPACE_SEPARATOR . \'posts_columns\');
function prefix_posts_custom_column($column_name, $post_ID)
{
if ($column_name == AMP_OPTION_VIEWS) {
echo prefix_get_count($post_ID);
}
}
add_action(\'manage_posts_custom_column\', __NAMESPACE__ . NAMESPACE_SEPARATOR . \'posts_custom_column\', 10, 2);
/**
* This function will return most viewed posts
*
* @return WP_Query
*/
function get_most_views($args)
{
global $wpdb;
$args[\'suppress_filters\'] = false;
$orderby_function = function ($orderby) {
global $wpdb;
return "{$wpdb->posts}.post_views DESC";
};
add_filter(\'posts_orderby\', $orderby_function);
$return = new WP_Query( $args );
if (stripos($wpdb->last_error, "posts.post_views\' in \'order clause\'") !== false) {
global $wpdb;
$exists = $wpdb->get_row("SELECT meta_id FROM {$wpdb->postmeta} WHERE meta_key = \'" . AMP_OPTION_VIEWS . "\' LIMIT 1");
// zisti meta_key, ak je stara tema je iny
$meta_key = $exists ? AMP_OPTION_VIEWS : \'_im_views\';
// prida sa novy stlpec
$add_views_column = "ALTER TABLE `{$wpdb->posts}` ADD COLUMN `post_views` INT NOT NULL DEFAULT \'0\';";
$wpdb->query($add_views_column);
// prida sa index pre stlpec
$add_views_index = "ALTER TABLE `{$wpdb->posts}` ADD INDEX `post_views` (`post_views`);";
$wpdb->query($add_views_index);
// updatnu sa views z metadat
$update_values = "UPDATE {$wpdb->posts}
SET post_views = IFNULL((SELECT meta_value FROM {$wpdb->postmeta}
WHERE post_id = ID AND meta_key = \'{$meta_key}\' LIMIT 1), 0);";
$wpdb->query($update_values);
// vymazu sa zbytocne metadata
if (!$wpdb->last_error) {
$update_values = "DELETE FROM {$wpdb->postmeta} WHERE meta_key = \'{$meta_key}\'";
$wpdb->query($update_values);
}
$return = new WP_Query( $args );
}
remove_filter(\'posts_orderby\', $orderby_function);
return $return;
}
function prefix_ajax()
{
$post_id = $_POST[\'postID\'];
$result = prefix_increment_count($post_id);
echo $result ? "ok" : "fail";
exit;
}
add_action("wp_ajax_nopriv_amp_views", __NAMESPACE__ . NAMESPACE_SEPARATOR . \'ajax\');
add_action("wp_ajax_amp_views", __NAMESPACE__ . NAMESPACE_SEPARATOR . \'ajax\');