Php WordPress从回调函数获取Post Meta

Php WordPress从回调函数获取Post Meta,php,wordpress,post,meta,Php,Wordpress,Post,Meta,我正在开发我的第一个WP插件,我被卡住了 我在内容编辑器下方的帖子页面上创建了一个自定义字段(字段1)。它可以正确保存。:) 添加媒体时,我在媒体库弹出窗口中创建了一个自定义字段(字段2)。它可以正确保存。:) 我想做的是使用字段1中的值作为字段2的默认值。 我怀疑问题出在附件字段到编辑回调函数中 我认为$post现在指的是实际的“文件附件帖子”,而不是帖子本身,因此当我引用保存的值时: $post_meta = get_post_meta( $post->ID ); 它实际上是在提取与

我正在开发我的第一个WP插件,我被卡住了

我在内容编辑器下方的帖子页面上创建了一个自定义字段(字段1)。它可以正确保存。:)

添加媒体时,我在媒体库弹出窗口中创建了一个自定义字段(字段2)。它可以正确保存。:)

我想做的是使用字段1中的值作为字段2的默认值。

我怀疑问题出在附件字段到编辑回调函数中

我认为$post现在指的是实际的“文件附件帖子”,而不是帖子本身,因此当我引用保存的值时:

$post_meta = get_post_meta( $post->ID );
它实际上是在提取与该附件相关的所有元,而不是当前帖子。有可能从实际帖子中提取meta吗

此代码来自法典:

function my_add_attachment_location_field( $form_fields, $post ) {
    $field_value = get_post_meta( $post->ID, 'location', true );
    $form_fields['location'] = array(
        'value' => $field_value ? $field_value : '',
        'label' => __( 'Location' ),
        'helps' => __( 'Set a location for this attachment' )
    );
    return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'my_add_attachment_location_field', 10, 2 );

function my_save_attachment_location( $attachment_id ) {
    if ( isset( $_REQUEST['attachments'][$attachment_id]['location'] ) ) {
        $location = $_REQUEST['attachments'][$attachment_id]['location'];
        update_post_meta( $attachment_id, 'location', $location );
    }
}
add_action( 'edit_attachment', 'my_save_attachment_location' );
我将如何为我们正在插入附件的当前帖子获取帖子元数据?这需要在上面的codex代码中的my_add_attachment_location_field回调函数中发生


谢谢

我能想到的一个方法是:

$actual\u post\u id=$post->post\u父项

然后你可以做:

获取帖子元($actual帖子id)


您可以尝试以下操作:

/**
 * Display custom 'location' attachment field
 */

function so_22850878_attachment_fields_to_edit( $form_fields, $post )
{
    $field_value = get_post_meta( $post->post_parent, 'location', true );

    $form_fields['location'] = array(
        'value' => $field_value ? $field_value : '',
        'label' => __( 'Location' ),
        'helps' => __( 'Set a location for this attachment' )
    );  

    return $form_fields;
}

add_filter( 'attachment_fields_to_edit', 
            'so_22850878_attachment_fields_to_edit', 10, 2 );

我不确定您想要的是哪种工作流程,但据我所知,您的想法存在问题:

更新媒体弹出窗口中的
位置
字段时,如下所示 您想更新父帖子的
位置
帖子元值。
但是,由于插入
如果将图像放入post editor,则您的
位置
值将被覆盖 更新帖子时使用旧值

因此,我想知道您是否可以使用隐藏的post元值,例如
\u location


希望这能有所帮助。

好的,尝试另一种方式……在导航器中,您将无法轻松获取帖子id。我不知道位置是什么,但是如果你想把图片保存为post-meta,我使用这个

步骤:

1.创建一个新的js文件+访问并将那里的js复制到js文件中。称之为miu_script.js,或者如果您想更改它,您需要对下面的代码进行一些修改。将其保存在插件文件夹中(若要将其移动到子文件夹中,请更改下面的路径)

  • 在函数中输入下面的代码-它会将图像位置保存为序列化url,并在名为“_images”的字段中再次更改,如果需要,请修改代码

  • 下面可能有错误,我有Oop格式的,所以请告诉我是否有问题。如果有,请注意php错误,如果没有php错误但它不起作用,请检查chrome或firefox中的控制台。我将能够调试

  • 在php函数中

    function add_image_meta_box() {
       add_meta_box(
                    'multi_image_upload_meta_box'
                    , __('Upload Multiple Images', 'miu_textdomain')
                    , 'render_meta_box_content'
                    , $post_type
                    , 'advanced'
                    , 'high'
            ); 
    }
    
    add_action( 'add_meta_boxes', 'add_image_meta_box' );
    
    
    
    
    function render_meta_box_content($post) {
    
        wp_nonce_field('miu_inner_custom_box', 'miu_inner_custom_box_nonce');
    
        $value = get_post_meta($post->ID, '_images', true); // <-- change field if wanted, there is 1 more below that will need the same name
    
        $metabox_content = '<div id="miu_images"></div><input type="button" onClick="addRow()" value="Add Image" class="button" />';
        echo $metabox_content;
    
        $images = unserialize($value); //<--- when using the images use this!!
    
        $script = "<script>
            itemsCount= 0;";
        if (!empty($images))
        {
            foreach ($images as $image)
            {
                $script.="addRow('{$image}');";
            }
        }
        $script .="</script>";
        echo $script;
    }
    
    
    
    
    
    function save_image($post_id){
    
        if (!isset($_POST['miu_inner_custom_box_nonce']))
            return $post_id;
    
        $nonce = $_POST['miu_inner_custom_box_nonce'];
    
        if (!wp_verify_nonce($nonce, 'miu_inner_custom_box'))
            return $post_id;
    
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
            return $post_id;
    
    
        if ('page' == $_POST['post_type']) {
    
            if (!current_user_can('edit_page', $post_id))
                return $post_id;
        } else {
    
            if (!current_user_can('edit_post', $post_id))
                return $post_id;
        }
    
        $posted_images = $_POST['miu_images'];
        $miu_images = array();
        foreach ($posted_images as $image_url) {
            if(!empty ($image_url))
                $miu_images[] = esc_url_raw($image_url);
        }
    
    
        update_post_meta($post_id, '_images', serialize($miu_images));<--if you changed this above.......make sure they match
      }
    
      add_action( 'save_post', 'save_image' );
    
    
      function enqueue_scripts($hook){
        if ('post.php' != $hook && 'post-edit.php' != $hook && 'post-new.php' != $hook)
            return;
        wp_enqueue_script('miu_script', plugin_dir_url(__FILE__) . 'miu_script.js', array('jquery')); //<--this is the path!!! change if wanted (prob a good idea to add to js folder)
    }
      add_action('admin_enqueue_scripts', 'enqueue_scripts');
    
    函数add\u image\u meta\u box(){
    添加元框(
    “多图像上传元框”
    ,uuu('上载多个图像','miu_textdomain')
    ,“渲染元框内容”
    ,$post_类型
    “高级”
    “高”
    ); 
    }
    添加动作(“添加元框”、“添加图像元框”);
    函数呈现\元\框\内容($post){
    wp_nonce_字段('miu_inner_custom_box','miu inner_custom_box_nonce');
    
    $value=get_post_meta($post->ID,'u images',true);//只是一个简单的解决方案

    function my_add_attachment_location_field( $form_fields, $post ) {
        $field_value = get_post_meta( $post->ID, 'location', true );
    
        $default = get_post_meta( $_REQUEST['post_id'] , 'location', true ) ? get_post_meta( $_REQUEST['post_id'] , 'location', true ): "Location Not Yet Set";
    
        $form_fields['location'] = array(
            'value' => $field_value ? $field_value : $default,
            'label' => __( 'Location' ),
            'helps' => __( 'Set a location for this attachment' )
        );
        return $form_fields;
    }
    add_filter( 'attachment_fields_to_edit', 'my_add_attachment_location_field', 10, 2 );
    
    使用我的插件可以很好地工作

    $\u请求['post\u id']是实际的wp post id 使用get_post_meta,您可以获取您的元数据库位置。。
    :)

    我不知道我是不是在猜测。你可以使用这个功能

        wp_reset_query();
        global $post;
        $post->ID // retrive the global $post id;
    

    谢谢,但这似乎不起作用。我也一直在四处搜索,似乎没有取得任何进展。嗯……我将使用var_dump($post)并查看是否设置了post_父属性。如果仍然不起作用,请尝试此操作。谢谢!我认为$post实际上是一个完全独立的“post”特定于图像/附件-而不是实际帖子本身?因此,我无法从媒体库弹出窗口中回显实际帖子ID。似乎您引用的函数需要帖子ID,但我会试一试。嘿@Aaron,$post in attachment\u fields\u to\u edit filter是媒体帖子(附件帖子类型)这包括关于附件的所有信息,如果您想检索附件上载到的当前帖子,$post->post\u家长回答正确。:|您有权访问您的数据库吗?实际上,您想在哪里使用它作为特色图片或其他图片?
    function my_add_attachment_location_field( $form_fields, $post ) {
        $field_value = get_post_meta( $post->ID, 'location', true );
    
        $default = get_post_meta( $_REQUEST['post_id'] , 'location', true ) ? get_post_meta( $_REQUEST['post_id'] , 'location', true ): "Location Not Yet Set";
    
        $form_fields['location'] = array(
            'value' => $field_value ? $field_value : $default,
            'label' => __( 'Location' ),
            'helps' => __( 'Set a location for this attachment' )
        );
        return $form_fields;
    }
    add_filter( 'attachment_fields_to_edit', 'my_add_attachment_location_field', 10, 2 );
    
        wp_reset_query();
        global $post;
        $post->ID // retrive the global $post id;