获取使用附件的所有帖子(任何帖子类型)

时间:2014-07-11 作者:kaiser

要计算一张图片所附的帖子数量并不容易,WordPress根本无法跟踪。它只是跟踪附件最初上传到的帖子(甚至不一定在那里使用)。

enter image description here

插件

为了让您尽快开始,以下是插件代码:

<?php
/**
 * Plugin Name: Media Count
 * Description: Adds a column to the media admin list table to show the count of posts
 */

add_filter( \'manage_media_columns\', function( $cols, $detached )
{
    $cols[\'count\'] = \'Count\';
    $cols[\'size\']  = \'Size\';
    return $cols;
}, 10, 2 );

add_action( \'manage_media_custom_column\', function( $col, $id )
{
    switch ( $col )
    {
        case \'size\' :
            $meta = wp_get_attachment_metadata( $id );
            // Image
            isset( $meta[\'width\'] )
                AND print "{$meta[\'width\']} &times; {$meta[\'height\']}";
            // Audio
            isset( $meta[\'bitrate\'] )
                AND print "{$meta[\'length_formatted\']} min";
            break;
        case \'count\' :
            $att  = get_post_custom( $id );
            $file = $att[\'_wp_attached_file\'][0];
            // Do not take full path as different image sizes could
            // have different month, year folders due to theme and image size changes
            $file  = pathinfo( $file, PATHINFO_FILENAME );
            // @TODO Fill in the blanks
            break;
    }
}, 10, 2 );
问题:如何计算附件使用的帖子数量-最有效的方法。

最终插件可以是downloaded as Gist here.

2 个回复
最合适的回答,由SO网友:Stephen Harris 整理而成

Second pass. 已知问题:

需要缓存(缓存需要尽可能整理)

  • 帖子类型是硬编码的
  • 我们对哪些帖子状态感兴趣
  • 功能如下:

    /**
     * Given an attachment ID, searches for any post with that attachment used
     * as a featured image, or if it is present in the content of the post.
     * (Note above known issues).
    */
    function get_image_count( $id ){
        global $wpdb;
    
        $att  = get_post_custom( $id );
        $file = $att[\'_wp_attached_file\'][0];
        //Do not take full path as different image sizes could
        // have different month, year folders due to theme and image size changes
        $file = sprintf( "%s.%s",
            pathinfo( $file, PATHINFO_FILENAME ),
            pathinfo( $file, PATHINFO_EXTENSION )
        );
    
        $sql = "SELECT {$wpdb->posts}.ID 
            FROM {$wpdb->posts} 
            INNER JOIN {$wpdb->postmeta} 
            ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id) 
            WHERE {$wpdb->posts}.post_type IN (\'post\', \'page\', \'event\') 
            AND (({$wpdb->posts}.post_status = \'publish\')) 
            AND ( ({$wpdb->postmeta}.meta_key = \'_thumbnail_id\' 
                AND CAST({$wpdb->postmeta}.meta_value AS CHAR) = \'%d\') 
                OR ( {$wpdb->posts}.post_content LIKE %s )
            ) 
            GROUP BY {$wpdb->posts}.ID";
    
        $prepared_sql = $wpdb->prepare( $sql, $id, "%src=\\"%".$wpdb->esc_like( $file )."\\"%" );
    
        $post_ids  = $wpdb->get_results( $prepared_sql );
    
        $count = count( $post_ids );
    
        return $count;
    }
    

    SO网友:Nicolai Grossherr

    作为补充,作为对不同图像大小的改进,我会:

    $file_name      = pathinfo( $file, PATHINFO_FILENAME );
    $file_extension = \'.\' . pathinfo( $file, PATHINFO_EXTENSION );
    
    而是将$file.

    并将SQL准备更改为:

    $prepared_sql =
        $wpdb->prepare(
            $sql,
            $id,
            "%src=\\"%"
                . like_escape( $file_name )
                . "%"
                . like_escape( $file_extension )
                . "\\"%"
        );
    

    作为第二个添加的示例,使用MySQL的REGEXP/RLIKE 功能,它还可以在a 标签,除此之外,它还能够——与第一个加法相反,第一个加法只获取完整的上传大小——获取独立于图像大小的图像——例如,»图像。jpg«将是全尺寸和»image-150x150。jpg«生成的尺寸-使用:

    $file_name      = pathinfo( $file, PATHINFO_FILENAME );
    // beware different syntax
    $file_extension = \'[[...]]\'.pathinfo( $file, PATHINFO_EXTENSION );
    
    $sql = "SELECT {$wpdb->posts}.ID 
        FROM {$wpdb->posts} 
        INNER JOIN {$wpdb->postmeta} 
        ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id) 
        WHERE {$wpdb->posts}.post_type IN (\'post\', \'page\', \'event\') 
        AND (({$wpdb->posts}.post_status = \'publish\')) 
        AND ( ({$wpdb->postmeta}.meta_key = \'_thumbnail_id\' 
            AND CAST({$wpdb->postmeta}.meta_value AS CHAR) = \'%d\') 
            OR ( {$wpdb->posts}.post_content REGEXP %s )
        ) 
        GROUP BY {$wpdb->posts}.ID";
    
    $exp =
        \'([[.<.]])\' // tag start
            . \'(img|a)\' // define tag types
            . \'.*\' // other attributes void
            . \'(src|href)\' // define anchor(s) attribute
            . \'=([[.".]]|[[.\\\'.]])\' // quotes
            . \'.*\' // path/URL void
            . $file_name
            . \'.*\' // image size void
            . $file_extension
            . \'([[.".]]|[[.\\\'.]])\' // quotes
            . \'.*\' // other attributes void
            . \'([[.>.]])\' // tag end
        ;
    
    $prepared_sql =
        $wpdb->prepare(
            $sql,
            $id,
            $exp
        );
    
    $post_ids  = $wpdb->get_results( $prepared_sql );
    

    结束

    相关推荐

    Media Manager(3.5版):如何在创建媒体框时显示空的媒体库?

    有没有办法确保“媒体库”选项卡在我开始在“上载文件”选项卡中上载一些图像之前不会显示任何图像?我正在创建wp。具有以下代码的媒体框:var myframe = wp.media({ title : \'Bla Bla\', frame : \'select\', library: { //query : false, // not working type : \'image\', },&#x