最新消息: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)

custom field - add meta box considers the selected option as post parent

matteradmin10PV0评论

I have added a meta box that will list specific post type posts, and on save it will save a meta key with selected post id, but the problem is that it rewrites the permalink of the post to consider the selected post as its parent, which i don't want to, i just need to store the selected post id. Where is the problem here:

Found solution: Answered below

function hfx_register_meta_boxes() {
    add_meta_box( 'select_hfx_workshop', __( 'Workshop for', HFX_DOMAIN ), 'hfx_select_workshop_parent_meta_box', 'forum', 'side',
            'core');
}
add_action( 'add_meta_boxes', 'hfx_register_meta_boxes' );


    function hfx_select_workshop_parent_meta_box(){
        global $post;
        $selected = '';

        if(get_post_meta( $post->ID, 'parent_id', true )){
            $selected = get_post_meta( $post->ID, 'workshop_id', true );
        }

        if(post_type_exists( 'product' )){
            $workshopsfx = get_posts(
            array(
                'post_type'   => 'product',
                'orderby'     => 'title',
                'order'       => 'ASC',
                'numberposts' => -1
            )
        );

        if ( !empty( $workshopsfx ) ) {

            echo '<select name="parent_id" class="widefat">'; // !Important! Don't change the 'parent_id' name attribute.
            echo '<option value="">'.esc_html__('Choose a workshop', HFX_DoMAIN).'</option>';
            foreach ( $workshopsfx as $workshop ) {
                printf( '<option value="%s"%s>%s</option>', esc_attr( $workshop->ID ), selected( $workshop->ID, $selected, false ), esc_html( $workshop->post_title ) );
            }

            echo '</select>';
        }
        }
    }

function hfx_save_meta_box( $post_id ) {
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    if ( $parent_id = wp_is_post_revision( $post_id ) ) {
        $post_id = $parent_id;
    }
    $fields = [
        'parent_id',
    ];
    foreach ( $fields as $field ) {
        if ( array_key_exists( $field, $_POST ) ) {
            update_post_meta( $post_id, $field, sanitize_text_field( $_POST[$field] ) );
        }
     }
}
add_action( 'save_post', 'hfx_save_meta_box' );

I have added a meta box that will list specific post type posts, and on save it will save a meta key with selected post id, but the problem is that it rewrites the permalink of the post to consider the selected post as its parent, which i don't want to, i just need to store the selected post id. Where is the problem here:

Found solution: Answered below

function hfx_register_meta_boxes() {
    add_meta_box( 'select_hfx_workshop', __( 'Workshop for', HFX_DOMAIN ), 'hfx_select_workshop_parent_meta_box', 'forum', 'side',
            'core');
}
add_action( 'add_meta_boxes', 'hfx_register_meta_boxes' );


    function hfx_select_workshop_parent_meta_box(){
        global $post;
        $selected = '';

        if(get_post_meta( $post->ID, 'parent_id', true )){
            $selected = get_post_meta( $post->ID, 'workshop_id', true );
        }

        if(post_type_exists( 'product' )){
            $workshopsfx = get_posts(
            array(
                'post_type'   => 'product',
                'orderby'     => 'title',
                'order'       => 'ASC',
                'numberposts' => -1
            )
        );

        if ( !empty( $workshopsfx ) ) {

            echo '<select name="parent_id" class="widefat">'; // !Important! Don't change the 'parent_id' name attribute.
            echo '<option value="">'.esc_html__('Choose a workshop', HFX_DoMAIN).'</option>';
            foreach ( $workshopsfx as $workshop ) {
                printf( '<option value="%s"%s>%s</option>', esc_attr( $workshop->ID ), selected( $workshop->ID, $selected, false ), esc_html( $workshop->post_title ) );
            }

            echo '</select>';
        }
        }
    }

function hfx_save_meta_box( $post_id ) {
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    if ( $parent_id = wp_is_post_revision( $post_id ) ) {
        $post_id = $parent_id;
    }
    $fields = [
        'parent_id',
    ];
    foreach ( $fields as $field ) {
        if ( array_key_exists( $field, $_POST ) ) {
            update_post_meta( $post_id, $field, sanitize_text_field( $_POST[$field] ) );
        }
     }
}
add_action( 'save_post', 'hfx_save_meta_box' );
Share Improve this question edited Dec 25, 2018 at 8:07 Mohamed Omar asked Dec 24, 2018 at 14:40 Mohamed OmarMohamed Omar 5191 gold badge5 silver badges17 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Actually the problem is that i was giving the select option a name of "parent_id", which for some how automatically used as post parent, but once i changed it to other name it worked.

Working code:

<?php
function hfx_register_meta_boxes() {
add_meta_box( 'select_hfx_workshop', __( 'Workshop for', HFX_DOMAIN ), 'hfx_select_workshop_parent_meta_box', 'forum', 'side','core');
}
add_action( 'add_meta_boxes_forum', 'hfx_register_meta_boxes' );


function hfx_select_workshop_parent_meta_box(){
    global $post;

    $selected = '';

    wp_nonce_field( 'select_workshophfx_box_action', 'select_workshophfx_box' );

    if(get_post_meta( $post->ID, 'workshop_id', true )){
        $selected = get_post_meta( $post->ID, 'workshop_id', true );
    }

    if(post_type_exists( 'product' )){
        $workshopsfx = get_posts(
        array(
            'post_type'   => 'product',
            'orderby'     => 'title',
            'order'       => 'ASC',
            'numberposts' => -1
        )
    );

    if ( !empty( $workshopsfx ) ) {


        echo '<select name="workshop_id" class="widefat">';
        echo '<option value="">'.esc_html__('Choose a workshop', HFX_DoMAIN).'</option>';
        foreach ( $workshopsfx as $workshop ) {
            printf( '<option value="%s"%s>%s</option>', esc_attr( $workshop->ID ), selected( $workshop->ID, $selected, false ), esc_html( $workshop->post_title ) );
        }

        echo '</select>';
    }
    }
}

function hfx_save_meta_box( $post_id ) {
if ( !isset( $_POST['select_workshophfx_box'] ) || !wp_verify_nonce( $_POST['select_workshophfx_box'], 'select_workshophfx_box_action' ) ){
    return;
}

if ( ! current_user_can( 'edit_post', $post_id ) ){
    return;
}


if ( $parent_id = wp_is_post_revision( $post_id ) ) {
    $post_id = $parent_id;
}
$fields = [
    'workshop_id',
];
foreach ( $fields as $field ) {
    if ( array_key_exists( $field, $_POST ) ) {
        update_post_meta( $post_id, $field, sanitize_text_field( $_POST[$field] ) );
    }
 }
}
Post a comment

comment list (0)

  1. No comments so far