0

I would like to show certain Custom Fields below my Editor. At the moment I only have athe dropdown field but there's so many metas inside which I am not using. By publishing 60-100 posts a day it is really time consuming searching for the respective custom field. enter image description here

I found the following code somewhere to permenantly display the Custom Field but it does not seem to work.

add_action( 'add_meta_boxes_tender', 'lamosty_add_tender_meta_data' );

function lamosty_add_tender_meta_data( WP_Post $tender ) {
        // Add the meta data you want the custom post type to have
    $tender_meta_data = [
        'briefing',
        'closing',
        'rfirfqnumber',
        'download'
    ];

    foreach ( $tender_meta_data as $meta ) {
        add_post_meta( $tender->ID, $meta, '', true );
    }
}
?>

My desired outcome is that every time I create a new Post the respective Custom Fields are always there and I don't need to look them up in the dropdown. For example like the picture below.

Pleas note that for every new post the values of the Custom Field change!

enter image description here

Do I or does my request make sense?

1 Answer 1

0

this should work but you overwrite the values for existing objects.

to add default values only for new objects try that :

$postType = "tender";

add_action( 'save_post_' . $postType, function ($post_ID, \WP_Post $tender, $update) {

    if (!$update) {
        // default value for new object

        // Add the meta data you want the custom post type to have
        $tender_meta_data = [
            'briefing',
            'closing',
            'rfirfqnumber',
            'download',
        ];

        foreach ( $tender_meta_data as $meta ) {
            add_post_meta( $tender->ID, $meta, '', true );
        }


        return;
    }



}, 10, 3);
5
  • overwrite as in deleting the value?
    – mad2kx
    Commented Apr 21, 2018 at 6:54
  • yes, when you edit an existing object, your code is run and the 4 values are set to ''.
    – mmm
    Commented Apr 21, 2018 at 8:54
  • OK that's not good. Is there a way around it without overwriting it? I think the other mistake is that I am "adressing: the code to a CPT but I am using normal posts, not CPT.
    – mad2kx
    Commented Apr 21, 2018 at 9:22
  • my code doesn't overwrite that and create only values for new objects. and to use with regular posts, start with $postType = "post";
    – mmm
    Commented Apr 21, 2018 at 9:45
  • OK thanks a lot. Tested it on another installation which is less important and it seems to work. Nothing got deleted so far
    – mad2kx
    Commented Apr 21, 2018 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.