WordPress

[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);
}

[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 );

[WordPress]doing_wp_cronが付いてたらリダイレクト

define(‘ALTERNATE_WP_CRON’, true);の時に発生

if(isset($_GET['doing_wp_cron'])){
    header('Location: '.get_current_url(false,array('doing_wp_cron')));
    exit;
}
public function get_current_url($remove_get_query = false,$exclude_keys = array())
{
    $suffix = $_SERVER['REQUEST_URI'];
    if($remove_get_query){
        $tmp = explode('?',$suffix);
        $suffix = array_shift($tmp);
    }else if(!empty($exclude_keys)){
        $tmp = explode('?',$suffix);
        $suffix = array_shift($tmp);
        $get = get_get_queries($exclude_keys);
        if(!empty($get))
            $suffix .= '?' . http_build_query($get);
    }
    return get_base_url($suffix);
}
public function get_get_queries($exclude_keys = array())
{
    $result = array();
    foreach ($_GET as $key => $value){
        if(!empty($exclude_keys) && in_array($key,$exclude_keys)){
            continue;
        }
        $result[$key] = $value;
    }
    return $result;
}
public function get_base_url($suffix = '')
{
    return (empty($_SERVER["HTTPS"]) ? "http://" : "https://") . $_SERVER["HTTP_HOST"] . $suffix;
}

[WordPress]Duplicate Post

カスタム投稿タイプで「複製」を実行すると「投稿」にリダイレクトされるのを修正

wp_redirect( add_query_arg( array( 'cloned' => 1, 'ids' => $post->ID), $sendback ) );

duplicate-post-admin.phpの315行目あたりの上記を下記へ変更

wp_redirect( add_query_arg( array( 'cloned' => 1, 'ids' => $post->ID, 'post_type' => $post->post_type), $sendback ) );

[WordPress]get_post_by_post_name

public function get_post_by_post_name($post_name, $post_type = '')
{
    global $wpdb;
    $sql = array();
    $sql[] = "SELECT";
    $sql[] = "p.*";
    $sql[] = "FROM $wpdb->posts AS p";
    $sql[] = "WHERE p.post_status = 'publish' AND p.post_name = '$post_name'";
    if (!empty($post_type))
        $sql[] = "AND p.post_type = '$post_type'";
    $result = $wpdb->get_results(implode(' ', $sql));
    if (empty($result))
        return null;
    return $result[0];
}

[WordPress]get_postsテンプレ

get_postsテンプレ

$conditions = array(
    'post_type' => 'カスタム投稿タイプ',
    'posts_per_page' => -1,//-1:全て取得する
    'post_status' => 'publish',
    'orderby' => 'date',
    'order' => 'DESC',
    'tax_query' => array(
        array(
            'taxonomy' => 'タクソノミー',
            'field' => 'slug',
            'terms' => 'ターム',
        )
    ),
);
$posts = get_posts($conditions);
<?php foreach($posts as $post):?>
<?php endforeach; wp_reset_postdata();?>