CMS

[WordPress]TransientAPI#3

    public function delete_transient($transient)
    {
        delete_transient($transient);
    }
    public function delete_all_transient()
    {
        global $wpdb;
        $sql = "DELETE FROM `{$wpdb->options}` WHERE `option_name` LIKE ('_transient_%');";
        $result = $wpdb->get_results($sql);
        $sql = "DELETE FROM `{$wpdb->options}` WHERE `option_name` LIKE ('_site_transient_%');";
        $result = $wpdb->get_results($sql);
    }

[WordPress]TransientAPI#2

public function get_transient_by_query($query)
{
    if(!$this->use_transient)
        return false;
    return $this->get_transient(md5($query));
}
public function get_transient($transient)
{
    if(!$this->use_transient)
        return false;
    return get_transient($transient);
}

[WordPress]TransientAPI#1

public function set_transient_by_query($query, $value, $expiration = -1)
{
    if(!$this->use_transient)
        return;
    $this->set_transient(md5($query),$value,$expiration);
}
public function set_transient($transient, $value, $expiration = -1)
{
    if(!$this->use_transient)
        return;
    if($expiration < 0)
        $expiration = $this->transient_expiration;
    set_transient($transient, $value, $expiration );
}

[WordPress]カスタム投稿タイプxxxx一覧にカラム追加

function add_columns_to_xxxxx( $columns )
{
    $new_columns = array();
    foreach ( $columns as $column_name => $column_display_name ) {
        if( $column_name == 'title' ) {
            $new_columns['ID'] = 'ID';
        }
        $new_columns[ $column_name ] = $column_display_name;
    }
    return $new_columns;
}
add_filter('manage_xxxxx_posts_columns' , 'add_columns_to_xxxxx');

function output_column_value_xxxxx( $column, $post_id )
{
    switch ( $column ) {
        case 'ID':
            echo $post_id;
            break;
    }
}
add_action( 'manage_xxxxx_posts_custom_column' , 'output_column_value_xxxxx', 10, 2 );

[WordPress]IDからACFProのカスタムフィールドの値を取得

$metas = $this->get_metas_by_post_id($ids, $cf_names);

public function get_metas_by_post_id($post_id, $cf_names = null)
{
    global $wpdb;
    $sql = array();
    $sql[] = "SELECT";

    $fields = array();
    $fields[] = "pm.post_id AS ID";
    $fields[] = "pm.meta_key";
    $fields[] = "pm.meta_value";
    $fields[] = "(CASE WHEN p2.guid IS NULL THEN '' ELSE p2.guid END) AS guid";
    $sql[] = implode(',', $fields);

    $sql[] = "FROM $wpdb->postmeta AS pm";

    $sql[] = "LEFT JOIN $wpdb->posts AS p ON p.ID = pm.post_id";
    $sql[] = "LEFT JOIN $wpdb->posts AS p2 ON pm.meta_value = p2.ID AND p2.post_type = 'attachment'";
    $sql[] = "WHERE pm.post_id " . $this->_get_sql_for_in($post_id, true);
    if (!empty($cf_names))
        $sql[] = "AND pm.meta_key " . $this->_get_sql_for_in($cf_names);
    $sql[] = "ORDER BY pm.post_id ASC";
    
    $result = $wpdb->get_results(implode(' ', $sql));
    $this->unserialize_by_keys($result);

    return $result;
}
public function unserialize_by_keys(&$post, $keys = null)
{
    if (is_array($post)) {
        foreach ($post AS $elm)
            $this->unserialize_by_keys($elm, $keys);
        return;
    }
    $target = !empty($keys) ? $keys : $post;
    foreach ($target as $key => $value)
        $post->$key = maybe_unserialize($post->$key);
}

[PHP,MT]phpでMTのデータ取得

phpでMTのデータ取得

public $basepath = '';
public $cfg_file = 'mt-config.cgi';
public $blog_id = null;
public $mt;

public function __construct($blog_id = null)
{
    $this->basepath = dirname(dirname(dirname(__FILE__))) . '/mt';
    set_include_path(get_include_path() . PATH_SEPARATOR . $this->basepath . '/php' . PATH_SEPARATOR . $this->basepath . '/php/extlib' . PATH_SEPARATOR . $this->basepath . '/MT/php/lib');
    require_once('mt.php');
    $this->init_mt($blog_id);
}

public function init_mt($blog_id = null)
{
    $this->blog_id = $blog_id;
    $this->mt = MT::get_instance($blog_id, $this->cfg_file);
}

public function get_custom_fields_by_entry_id($entry_id)
{
    $sql[] = "SELECT";
    $sql[] = "entry_meta_entry_id as entry_id,";
    $sql[] = "REPLACE(entry_meta_type,'field.','') as name,";
    $sql[] = "(CASE";
    $sql[] = "WHEN entry_meta_vchar IS NOT NULL THEN entry_meta_vchar";
    $sql[] = "WHEN entry_meta_vchar_idx IS NOT NULL THEN entry_meta_vchar_idx";
    $sql[] = "WHEN entry_meta_vdatetime IS NOT NULL THEN entry_meta_vdatetime";
    $sql[] = "WHEN entry_meta_vdatetime_idx IS NOT NULL THEN entry_meta_vdatetime_idx";
    $sql[] = "WHEN entry_meta_vinteger IS NOT NULL THEN entry_meta_vinteger";
    $sql[] = "WHEN entry_meta_vinteger_idx IS NOT NULL THEN entry_meta_vinteger_idx";
    $sql[] = "WHEN entry_meta_vfloat IS NOT NULL THEN entry_meta_vfloat";
    $sql[] = "WHEN entry_meta_vfloat_idx IS NOT NULL THEN entry_meta_vfloat_idx";
    $sql[] = "WHEN entry_meta_vblob IS NOT NULL THEN entry_meta_vblob";
    $sql[] = "WHEN entry_meta_vclob IS NOT NULL THEN entry_meta_vclob";
    $sql[] = "ELSE NULL END) as value";
    $sql[] = "FROM mt_entry_meta WHERE entry_meta_entry_id " . $this->_get_sql_for_in($entry_id,true);
    $sql = implode(' ',$sql);
    $records = $this->mt->db()->execute($sql);
    $records = $this->get_array_by_records($records);
    
    $tmp = array();
    if(!is_array($entry_id)){
        foreach ($records as $rec) {
            $tmp[$rec['name']] = $rec['value'];
        }
        return $tmp;
    }
    
    return $records;
}
protected function _get_sql_for_in($values, $is_number = false)
{
    if ($is_number)
        return is_array($values) && 1 < count($values) ? "IN (" . implode(",", $values) . ")" : (is_array($values) ? "= {$values[0]}" : "= $values");
    return is_array($values) && 1 < count($values) ? "IN ('" . implode("','", $values) . "')" : (is_array($values) ? "= '{$values[0]}'" : "= '$values'");
}
public function get_array_by_records($records)
{
    $result = array();
    if (!$records){
    }else {
        while ($item = $records->fetchRow()) {
            $result[] = $item;
        }
    }
    return $result;
}

[WordPress]メインクエリ停止

//メインクエリ停止
function custom_posts_request( $sql, &$query ) {
    if ( is_admin() || !$query->is_main_query() ){
        return $sql;
    }
    if ( $query->is_main_query() ) {
        /* prevent SELECT FOUND_ROWS() query*/
        $query->query_vars['no_found_rows'] = true;
        /* prevent post term and meta cache update queries */
        $query->query_vars['cache_results'] = false;
        return false;
    }
    return $sql;
}
//posts_request : post 配列を返す SQL クエリを実行する直前に、クエリ全体に対して適用される。
add_filter( 'posts_request', 'custom_posts_request', 10, 2 );