2013-03-30 109 views
0

我想自定義字段做一個「獨家」後,即只能在一個時間分配到一個職位。理想情況下直接從儀表板。使用自定義字段

比方說,我有一個自定義字段featured post/post-123價值true
如果我將featured post: true指定爲post-111,則post-123featured post的值爲自定義字段需要獲取值false或完全刪除。

這意味着我的自定義字段只能分配給一個帖子。
OR
定製字段用指定的值僅可以存在一次。

是否有一個插件,能做到嗎?或者是否可以使用WordPress插件Types

+0

也許你可以看它從另一個角度,並使其成爲網站級別的設置'特色 - 職位',有VA lue'post-123'?雖然我不太熟悉WordPress。 –

+0

是的,那工作,以及,但我不知道如何使網站級別設置。 我有一個第三個想法:一個簡單的側邊欄部件在那裏我可以選擇一個職位 - 這將幫助我爲好。不幸的是,只有插件/小部件顯示了特定類別的最新x個帖子。 – 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'); 
    } 

}