Metafields自定义字段acfзадатьзначениекастомфилдапоумолчанию

php
阅读 43 收藏 0 点赞 0 评论 0

wp-metafields
action add_post_meta set value set meta value pre_get_posts

add_action('wp_insert_post', 'set_default_custom_field');
function set_default_custom_field($post_id)
{
	$types = array('page','locationscontent','menu','gallery','parties','signup','jobs');
	if (in_array($_GET['post_type'], $types)){
		add_post_meta($post_id, 'post_order', '0', true);
	}
  return true;
}

function register_custom_fields( $post_ID ) {
    //Do nonce checking here
    if( !wp_is_post_revision( $post_ID ) ) {
        if('post' === $_REQUEST['post_type']){
						$featured = apply_filters('get_the_content', get_post_meta(get_the_ID(), 'featured', true));
						$primary = apply_filters('get_the_content', get_post_meta(get_the_ID(), 'primary', true));
						if(!empty($primary)){
							$post_view_status = 'primary';
						}
						else{
							$post_view_status = 'secondary';
						}
            update_post_meta( $post_ID, 'post_view_status', $post_view_status);
        }
    }
}
add_action('save_post', 'register_custom_fields');

function register_custom_fields( $post_ID ) {
    //Do nonce checking here
    if( !wp_is_post_revision( $post_ID ) ) {
        if('post' === $_REQUEST['post_type']){
						$featured = apply_filters('get_the_content', get_post_meta(get_the_ID(), 'featured', true));
						$primary = apply_filters('get_the_content', get_post_meta(get_the_ID(), 'primary', true));
						
						if(!empty($primary) && strtolower($primary) == 'yes'){
							$post_view_status = 'primary';
							update_post_meta( $post_ID, 'post_view_status', $post_view_status);
						}elseif(!empty($primary) && strtolower($primary) != 'yes'){
							delete_post_meta($post_ID, 'post_view_status');
						}
						
						if(!empty($featured) && strtolower($featured) == 'yes' && empty($primary)){
							$post_view_status = 'secondary';
							update_post_meta( $post_ID, 'post_view_status', $post_view_status);
						}elseif(!empty($featured) && strtolower($featured) != 'yes' && empty($primary)){
							delete_post_meta($post_ID, 'post_view_status');
						}
						
						if(empty($featured) && empty($primary)){
							delete_post_meta($post_ID, 'post_view_status');
						}
        }
    }
}
add_action('save_post', 'register_custom_fields');

function sticky_field( $post_ID ) {
    //Do nonce checking here
    if( !wp_is_post_revision( $post_ID ) ) {
        if('post' === $_REQUEST['post_type']){
					$stickies = get_option('sticky_posts');
					if (in_array($post_ID, $stickies)){
						update_option( 'sticky_posts', array());
						update_option( 'sticky_posts', array($post_ID));
					}
        }
    }
}
add_action('save_post', 'sticky_field');

add_action('pre_get_posts', 'app_close_date_query' );

function app_close_date_query( $wp_query )
{
    if (!is_admin())
    {
        if(is_archive() || is_tax())
        {
             set_query_var('meta_query',array(array('key' => 'app_close_date', 'value' => strtotime(date('m/d/Y')), 'compare' => '>=')) );
        }
    }
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号