2013-03-30 4 views
0

한 번에 하나의 게시물에만 지정할 수있는 사용자 정의 필드가 필요합니다. Dashboard에서 직접적으로 이상적입니다.사용자 정의 필드를 사용하여 "독점"게시물 만들기

사용자 입력란 이 /post-123이고 값이 true 인 경우를 가정 해 봅니다.
post-111featured post: true을 할당하면 post-123의 값인 featured post -custom 필드의 값이 false이거나 완전히 삭제되어야합니다.

즉, 내 사용자 정의 필드는 하나의 게시물에만 할당 될 수 있습니다.
또는
지정된 값이있는 사용자 지정 필드는 한 번만 존재할 수 있습니다.

플러그인이 있습니까? 아니면 WordPress 플러그인 Types으로 가능합니까?

+0

이 아마도 당신이에서 볼 수 있었다를 다른 각도로 설정하고 va가있는 사이트 수준 설정 인 '추천 게시물'으로 설정하십시오. lue'post-123'? 나는 WordPress에 너무 익숙하지 않다. –

+0

네, 사이트 수준 설정 방법을 잘 모르겠습니다. 세 번째 아이디어가 있습니다. 하나의 포스트를 선택할 수있는 간단한 사이드 바 위젯이 저에게 도움이 될 것입니다. 불행히도 특정 카테고리의 최신 게시물을 표시하는 플러그인/위젯 만 있습니다. – ProblemsOfSumit

답변

0

사용자 지정 필드 저장/업데이트/제거는 동작 후크 save_post에서 수행됩니다.

현재 게시물 메타 데이터를 업데이트하기 전에 동일한 메타를 포함 할 수있는 다른 모든 게시물을 쿼리하고 대/소문자를 제거합니다. 기본적으로

:

if (isset($_POST['exclusive_post']) ) 
{ 
    // CHECK FOR OTHER POSTS THAT HAVE THE META DATA SET 
    $args = array(
     'numberposts'  => -1, 
     'offset'   => 0, 
     'meta_key'  => 'exclusive_post', 
     'post_type'  => 'post', 
     'post_status'  => 'publish' 
    ); 
    $results = get_posts($args); 
    foreach($results as $other_post) 
    { 
     // REMOVE THE META DATA FROM OTHER POSTS 
     delete_post_meta($other_post->ID, 'exclusive_post'); 
    } 

    // UPDATE THE CURRENT POST META DATA 
    update_post_meta($post_id, 'exclusive_post', $_POST['exclusive_post']); 
} 
else 
{ 
     // NO POST META DATA IN CURRENT POST, REMOVE META 
    delete_post_meta($post_id, 'exclusive_post'); 
} 

그리고 여기, Add a checkbox to post screen that adds a class to the title에 기반을 둔 메타 상자를 사용하여 전체 작업 예 :

enter image description here

<?php 
/** 
* Plugin Name: Exclusive Post Meta Box 
* Description: Creates a custom field that can only be assigned to one post. 
* Plugin URI: http://stackoverflow.com/q/15721612/1287812 
* Author:  brasofilo 
* Author URI: https://wordpress.stackexchange.com/users/12615/brasofilo 
*/ 


/* Define the custom box */ 
add_action('add_meta_boxes', 'wpse_61041_add_custom_box'); 

/* Do something with the data entered */ 
add_action('save_post', 'wpse_61041_save_postdata', 10, 2); 

/* Adds a box to the main column on the Post and Page edit screens */ 
function wpse_61041_add_custom_box() { 
    add_meta_box( 
     'wpse_61041_sectionid', 
     'Exclusive Post', 
     'wpse_61041_inner_custom_box', 
     'post', 
     'side', 
     'high' 
    ); 
} 

/* Prints the box content */ 
function wpse_61041_inner_custom_box($post) 
{ 
    // Use nonce for verification 
    wp_nonce_field(plugin_basename(__FILE__), 'wpse_61041_noncename'); 

    // Get saved value, if none exists, "default" is selected 
    $saved = get_post_meta($post->ID, 'exclusive_post', true); 

    printf(
     '<input type="checkbox" name="exclusive_post" value="exclusive_post" id="exclusive_post" %1$s />'. 
     '<label for="exclusive_post"> This is the one.' . 
     '</label><br>', 
     checked($saved, 'exclusive_post', false) 
    ); 

} 

/* When the post is saved, saves our custom data */ 
function wpse_61041_save_postdata($post_id, $post_object) 
{ 
     // verify if this is an auto save routine. 
     // If it is our form has not been submitted, so we dont want to do anything 
     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) 
      return; 

     // verify this came from the our screen and with proper authorization, 
     // because save_post can be triggered at other times 
     if (!wp_verify_nonce($_POST['wpse_61041_noncename'], plugin_basename(__FILE__))) 
      return; 

    // don't run if saving revision. 
    if ('revision' == $post_object->post_type) 
     return; 

    if (isset($_POST['exclusive_post']) ) 
    { 
     // CHECK FOR OTHER POSTS THAT HAVE THE META DATA SET 
     $args = array(
      'numberposts'  => -1, 
      'offset'   => 0, 
      'meta_key'  => 'exclusive_post', 
      'post_type'  => 'post', 
      'post_status'  => 'publish' 
     ); 
     $results = get_posts($args); 
     foreach($results as $other_post) 
     { 
      // REMOVE THE META DATA FROM OTHER POSTS 
      delete_post_meta($other_post->ID, 'exclusive_post'); 
     } 

     // UPDATE THE CURRENT POST META DATA 
     update_post_meta($post_id, 'exclusive_post', $_POST['exclusive_post']); 
    } 
    else 
    { 
     // NO POST META DATA IN CURRENT POST, REMOVE META 
     delete_post_meta($post_id, 'exclusive_post'); 
    } 

} 
관련 문제