$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - Where to put meta Keys|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - Where to put meta Keys

matteradmin8PV0评论

Hello im creating my custom checkbox in gallery and i already add that , but save doesn't work , can someone take a look and tell me where should i put meta keys ?

function wporg_add_custom_box()
{
$screens = ['attachment'];
foreach ($screens as $screen) {
    add_meta_box(
        'wporg_box_id',           // Unique ID
        'Custom Meta Box Title',  // Box title
        'wporg_custom_box_html',  // Content callback, must be of type callable
        $screen                   // Post type
    );
}
}
add_action('add_meta_boxes', 'wporg_add_custom_box');


function wporg_custom_box_html($post)
{
$value = get_post_meta($post->ID, '_wporg_meta_key', true);
 ?>
<input type="checkbox" name="include_in_image_gallery" <?php selected($value, $post->ID); ?> >
 <?php
}

 function wporg_save_postdata($post_id)
{
 if (array_key_exists('wporg_field', $_POST)) {
    update_post_meta(
        $post_id,
        '_wporg_meta_key',
        $_POST['include_in_image_gallery']
    );
}
}
add_action('save_post', 'wporg_save_postdata');

My meta key name is: include_in_image_gallery

I want to use that meta in wp_query in another subpage.

Any idea? Thanks and best regards.

Hello im creating my custom checkbox in gallery and i already add that , but save doesn't work , can someone take a look and tell me where should i put meta keys ?

function wporg_add_custom_box()
{
$screens = ['attachment'];
foreach ($screens as $screen) {
    add_meta_box(
        'wporg_box_id',           // Unique ID
        'Custom Meta Box Title',  // Box title
        'wporg_custom_box_html',  // Content callback, must be of type callable
        $screen                   // Post type
    );
}
}
add_action('add_meta_boxes', 'wporg_add_custom_box');


function wporg_custom_box_html($post)
{
$value = get_post_meta($post->ID, '_wporg_meta_key', true);
 ?>
<input type="checkbox" name="include_in_image_gallery" <?php selected($value, $post->ID); ?> >
 <?php
}

 function wporg_save_postdata($post_id)
{
 if (array_key_exists('wporg_field', $_POST)) {
    update_post_meta(
        $post_id,
        '_wporg_meta_key',
        $_POST['include_in_image_gallery']
    );
}
}
add_action('save_post', 'wporg_save_postdata');

My meta key name is: include_in_image_gallery

I want to use that meta in wp_query in another subpage.

Any idea? Thanks and best regards.

Share Improve this question edited Jan 8, 2019 at 23:22 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Jan 8, 2019 at 22:45 MariuszMariusz 234 bronze badges 3
  • Is there any reason why the names of keys, fields, variables and functions have no meaning at all? ;) – Krzysiek Dróżdż Commented Jan 8, 2019 at 23:20
  • Yes , its from tutorial but with select , options. i will change that after all ; p – Mariusz Commented Jan 8, 2019 at 23:25
  • It would be a lot easier to understand that code and help you, if the names were meaningful ;) – Krzysiek Dróżdż Commented Jan 8, 2019 at 23:30
Add a comment  | 

1 Answer 1

Reset to default 0

First of all you have an if statement that is always false in your save function.

I don’t see any input called wporg_field anywhere in your meta box, so most probably there is no such field - so that condition is false and no meta field is saved.

Another problem is that you make wrong assumptions on how checkboxes work. If checkbox is not checked, then it won’t be sent in POST array.

Here’s a fixed version - it should work:

function wporg_save_postdata($post_id)
{
    if ( array_key_exists('include_in_image_gallery', $_POST) ) {
        update_post_meta(
            $post_id,
            '_wporg_meta_key',
            $_POST['include_in_image_gallery']
        );
    } else {
        delete_post_meta($post_id, '_wporg_meta_key');
    }
}

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far