Kazi Rabiul Islam Kazi Rabiul Islam - 3 years ago 253
PHP Question

In WordPress how to write post mata in a preper way?

Can anyone tell me, How to write post meta array in WordPress in a proper way?

Would this work:

'name' => esc_html__( 'Main Menu', 'adeq' ),
'id' => $prefix.'main_menu_default',
'type' => 'select',
'default' => 'default',
'options' => $nav_menus

Answer Source

Use update_post_meta to save data to the database. It will create the post meta field for the specific post if it does not exist and update it if it does.

update_post_meta( $post_id, $meta_key, $meta_value, $prev_value );

$prev_value is optional and can be used to update a specific value if you have multiple values with the same key.

Use get_post_meta to read them again.

get_post_meta( $post_id, $key = '', $single = false )

$key and $single are optional. If you don't pass a specific key in, you will get all meta fields and their values for the specified post. If you leave $single out or pass false, you will get an array back. Pass true as the third parameter and you will get a single value that is ready to be printed or worked with, e.g.

print get_post_meta(get_the_ID(), "my-metafield", true);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download