自定义帖子类型ID问题 - 查询其他帖子类型时

时间:2012-10-05 10:18:28

标签: wordpress custom-post-type meta-boxes

我有一个很复杂的问题要解决,这让我在最后一天左右感到困惑。

基本上,我有两种自定义帖子类型'Sessions'和'Recipes'。这些在WordPress的后端显示正常。

附加到会议的是几个元框,“介绍视频”,“会话视频”,“目标”,“健身水平”和“特色食谱”。

“精选食谱”元框是特殊的,因为它查询WordPress数据库以获取“食谱”自定义帖子类型中的所有帖子,然后使用这些值填充选择框。

现在,您可以从附加的屏幕截图中看到,配方自定义帖子类型的选择框正在提取正确的项目和ID,并且还会保存数据并拉回正确的选择并将其与正确的选项相关联“会话”帖子类型的帖子ID。太好了!

问题出现在帖子中的其他字段中,它们似乎使用第一个食谱ID作为拉取每个字段的数据的值,而不是“会话”自定义帖子类型ID。

现在我可以通过基于特定查询参数编写各种while循环来实现此功能,但这会产生另一个问题。数据保存在正确的位置,但它使用第一个“会话”cpt ID来撤回所有帖子的数据。

现在,我相信它与'recipe'元框创建有关,因为只要你从functions.php文件中删除这个函数及其相关函数,一切都可以正常工作。

我想我应该使用for循环而不是while循环,我现在要尝试这个。

希望你们中的一些PHP大师能够指出我正确的方向。我已经包含了我的functions.php文件和管理员的屏幕截图。欢呼声。

<?php

add_action( 'init', 'tfp_session_cpt' );
function tfp_session_cpt() {
    register_post_type( 'session',
        array(
            'labels' => array(
                'name' => __( 'Sessions' ),
                'singular_name' => __( 'Session' )
            ),
        'public' => true,
        'has_archive' => true,
        )
    );
}

add_action( 'init', 'tfp_recipe_cpt' );
function tfp_recipe_cpt() {
    register_post_type( 'recipe',
        array(
            'labels' => array(
                'name' => __( 'Recipes' ),
                'singular_name' => __( 'Recipe' )
            ),
        'public' => true,
        'has_archive' => true,
        )
    );
}

/*add_action( 'init', 'tfp_tip_cpt' );
function tfp_tip_cpt() {
    register_post_type( 'tip',
        array(
            'labels' => array(
                'name' => __( 'Tips' ),
                'singular_name' => __( 'Tip' )
            ),
        'public' => true,
        'has_archive' => true,
        )
    );
}*/

add_action( 'add_meta_boxes', 'tfp_add_introduction_video' );
add_action( 'add_meta_boxes', 'tfp_add_session_video' );
add_action( 'add_meta_boxes', 'tfp_add_goals' );
add_action( 'add_meta_boxes', 'tfp_add_levels' );
add_action( 'add_meta_boxes', 'tfp_add_recipes' );
/*add_action( 'add_meta_boxes', 'tfp_add_tips' );*/
add_action( 'save_post', 'tfp_save_introduction_video');
add_action( 'save_post', 'tfp_save_session_video');
add_action( 'save_post', 'tfp_save_goals');
add_action( 'save_post', 'tfp_save_levels');
add_action( 'save_post', 'tfp_save_recipes');
/*add_action( 'save_post', 'tfp_save_tips');*/

function tfp_add_introduction_video() {
    add_meta_box('tfp_introduction_video', 'Introduction Video', 'tfp_introduction_video_html', 'session');
}

function tfp_add_session_video() {
    add_meta_box('tfp_session_video', 'Session Video', 'tfp_session_video_html', 'session');
}

function tfp_add_goals() {
    $types = array ('session');
    foreach ($types as $type) {
        add_meta_box('tfp_goals', 'Goals', 'tfp_goals_html', $type);
    }
}

function tfp_add_levels() {
    add_meta_box('tfp_levels', 'Fitness Levels', 'tfp_levels_html', 'session');
}

function tfp_add_recipes() {
    add_meta_box('tfp_recipes', 'Select a Featured Recipe', 'tfp_recipes_html', 'session', 'side');
}

function tfp_add_tips() {
    add_meta_box('tfp_tips', 'Tips', 'tfp_tips_html', 'session', 'side');
}
function tfp_introduction_video_html($post) {

    // Nonce field for some validation
    wp_nonce_field( plugin_basename( __FILE__ ), 'session' );

    global $post;

    $introductionVideo = get_post_meta( $postID, 'introduction_video_embed_code', true );

    echo $post->ID;

    ?>
    <label for="introduction_video_embed_code">YouTube Embed Code</label>
    <textarea class="large-text code" id="introduction_video_embed_code" name="introduction_video_embed_code" value="<?php echo $introductionVideo; ?>"><?php echo get_post_meta( $post->ID, 'introduction_video_embed_code', true ); ?></textarea>

<?php }

function tfp_session_video_html($post) {

    // Nonce field for some validation
    wp_nonce_field( plugin_basename( __FILE__ ), 'session' );

    global $post;

    $sessionVideo = get_post_meta( $post->ID, 'session_video_embed_code', true ); ?>

    <?php echo $post->ID; ?>
    <label for="session_video_embed_code">YouTube Embed Code</label>
    <textarea class="large-text code" id="session_video_embed_code" name="session_video_embed_code" value="<?php echo $sessionVideo; ?>"><?php echo $sessionVideo; ?></textarea>
<?php }

function tfp_goals_html($post) {

    // Nonce field for some validation
    wp_nonce_field( plugin_basename( __FILE__ ), 'session' );

    $goals = get_post_meta( $post->ID, 'goals', true ); ?>

    <?php echo $post->ID; ?>

    <label for="weight_loss">
        <input type="checkbox" name="goal[]" id="weight_loss" value="Weight Loss" <?php if (in_array('Weight Loss', $goals)) echo "checked='checked'"; ?> />
        <?php _e("Weight Loss"); ?>
    </label>
    <br />
    <label for="improve_fitness">
        <input type="checkbox" name="goal[]" id="improve_fitness" value="Improve Fitness" <?php if (in_array('Improve Fitness', $goals)) echo "checked='checked'"; ?> />
        <?php _e("Improve Fitness"); ?>
    </label>
    <br />
    <label for="improve_health">
        <input type="checkbox" name="goal[]" id="improve_health" value="Improve Health" <?php if (in_array('Improve Health', $goals)) echo "checked='checked'"; ?> />
        <?php _e("Improve Health"); ?>
    </label>
    <br />
<?php }

function tfp_levels_html($post) {

    // Nonce field for some validation
    wp_nonce_field( plugin_basename( __FILE__ ), 'session' );

    $levels = get_post_meta( $post->ID, 'levels', true ); ?>

    <?php echo $post->ID; ?>

    <label for="beginner">
        <input type="checkbox" name="level[]" id="beginner" value="Beginner" <?php if (in_array('Beginner', $levels)) echo "checked='checked'"; ?> />
        <?php _e("Beginner"); ?>
    </label>
    <br />
    <label for="advanced">
        <input type="checkbox" name="level[]" id="advanced" value="Advanced" <?php if (in_array('Advanced', $levels)) echo "checked='checked'"; ?> />
        <?php _e("Advanced"); ?>
    </label>
    <br />
<?php }

function tfp_recipes_html($post) {

    // Nonce field for some validation
    wp_nonce_field( plugin_basename( __FILE__ ), 'session' );

    global $post;

    $recipes = get_post_meta( $post->ID, 'recipe_name', true );

    $recipeArgs = array(
        'post_type' => 'recipe'
    );
    $recipeQuery = new WP_Query($recipeArgs); ?>

    <?php echo $post->ID; ?>

    <select name="recipe_names" id="recipes">
        <option value="null">Select a Featured Recipe</option>
        <?php while ( $recipeQuery->have_posts() ) : $recipeQuery->the_post(); ?>
            <option id="<?php echo $post->ID; ?>" value="<?php the_title(); ?>" <?php $title = get_the_title(); if($recipes == $title) echo "selected='selected'";?>><?php the_title(); ?></option>
        <?php endwhile; ?>
    </select>
<?php }

/*function tfp_tips_html($post) {

    // Nonce field for some validation
    wp_nonce_field( plugin_basename( __FILE__ ), 'session' );

    global $post;

    $tipArgs = array(
        'post_type' => 'tip'
    );
    $tipQuery = new WP_Query($tipArgs); ?>

    <?php echo $post->ID; ?>

    <?php while ( $tipQuery->have_posts() ) : $tipQuery->the_post();

        $tipID = 'tip_' . $post->ID; 
        $tips = get_post_meta( $post->ID, $tipID, true );?>
        <?php echo $tipID; ?>

        <label for="<?php echo $tipID; ?>">
            <input type="checkbox" name="<?php echo $tipID; ?>" id="<?php echo $tipID; ?>" value="1" <?php if ($tips == 1) echo "checked='checked'"; ?> />
            <?php the_title(); ?>
        </label>
        <br />
    <?php endwhile;
}*/

function tfp_save_introduction_video($post_id) {

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

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }

    update_post_meta($post_id, 'introduction_video_embed_code', $_POST['introduction_video_embed_code']);
}

function tfp_save_session_video($post_id) {

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

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }

    update_post_meta($post_id, 'session_video_embed_code', $_POST['session_video_embed_code']);
}

function tfp_save_goals($post_id) {

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

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }

    update_post_meta($post_id, 'goals', $_POST['goal']);
}

function tfp_save_levels($post_id) {

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

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }

    update_post_meta($post_id, 'levels', $_POST['level']);
}

function tfp_save_recipes($post_id) {

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

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }

    update_post_meta($post_id, 'recipe_name', $_POST['recipe_names']);
}

/*function tfp_save_tips($post) {

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

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }

    if ( ! wp_verify_nonce( $_POST['custom_post_type'], plugin_basename(__FILE__) ) ) return;

    global $post;

    $tipArgs = array(
        'post_type' => 'tip'
    );

    $tipQuery = new WP_Query($tipArgs);

    while ( $tipQuery->have_posts() ) : $tipQuery->the_post();
        $tipID = 'tip_' . $post->ID;

        update_post_meta($post->ID, $tipID, $_POST[$tipID]);
    endwhile;
}*/

// Extra user profile fields, specifically goal and fitness level checkboxes and radio buttons
add_action( 'show_user_profile', 'gaf_user_profile_fields' );
add_action( 'edit_user_profile', 'gaf_user_profile_fields' );

// Function to generate field HTML
function gaf_user_profile_fields( $user ) { ?>
    <h3><?php _e("Goals and Fiteness Level", "blank"); ?></h3>

    <table class="form-table">
        <tr>
            <th scope="row"><?php _e("Goals"); ?></th>
            <td>
                <fieldset>
                    <legend class="screen-reader-text">
                        <span><?php _e("Goals"); ?></span>
                    </legend>
                    <?php $single = true; ?>
                    <?php $goals = get_user_meta( $user->ID, 'goals', $single ); ?>
                    <label for="weight_loss">
                        <input type="checkbox" name="goal[]" id="weight_loss" value="Weight Loss" <?php if (in_array('Weight Loss', $goals)) echo "checked='checked'"; ?> />
                        <?php _e("Weight Loss"); ?>
                    </label>
                    <br />
                    <label for="improve_fitness">
                        <input type="checkbox" name="goal[]" id="improve_fitness" value="Improve Fitness" <?php if (in_array('Improve Fitness', $goals)) echo "checked='checked'"; ?> />
                        <?php _e("Improve Fitness"); ?>
                    </label>
                    <br />
                    <label for="improve_health">
                        <input type="checkbox" name="goal[]" id="improve_health" value="Improve Health" <?php if (in_array('Improve Health', $goals)) echo "checked='checked'"; ?> />
                        <?php _e("Improve Health"); ?>
                    </label>
                    <br />
                </fieldset>
            </td>
        </tr>
        <tr>
            <?php $fitnessLevel = get_the_author_meta('fitness_level', $user->ID); ?>
            <th scope="row"><?php _e("Fitness Level"); ?></th>
            <td>
                <fieldset>
                    <legend class="screen-reader-text">
                        <span><?php _e("Fitness Level"); ?></span>
                    </legend>
                    <input class="tog" type="radio" name="fitness_level" value="beginner" <?php if ($fitnessLevel == 'beginner') { ?>checked="checked"<?php } ?> /><label>Beginner</label>
                    <br />
                    <input class="tog" type="radio" name="fitness_level" value="advanced" <?php if ($fitnessLevel == 'advanced') { ?>checked="checked"<?php } ?> /><label>Advanced</label>
                    <br />
                </fieldset>
            </td>
        </tr>
    </table>
<?php }

// Function and actions to save the data from the fields
add_action( 'personal_options_update', 'save_gaf_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_gaf_user_profile_fields' );

function save_gaf_user_profile_fields( $user_id ) {

    if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }

    update_user_meta( $user_id, 'goals', $_POST['goal'] );
    update_user_meta( $user_id, 'fitness_level', $_POST['fitness_level'] );
}

?>

enter image description here

[编辑]

我当然越来越接近这个功能,但它并不完全正确,因为它正在通过相同的标题。

function tfp_recipes_html($post) {

    // Nonce field for some validation
    wp_nonce_field( plugin_basename( __FILE__ ), 'session' );

    $recipeArgs = array(
        'post_type' => 'recipe'
    );
    $recipeQuery = get_posts($recipeArgs);

    $recipeName = get_post_meta( $post->ID, 'recipe_name', true );

    $recipeTitle[] = get_query_var('name');

    echo $recipeTitle;

    ?>

    <select name="recipe_names" id="recipes">
        <option value="null">Select a Featured Recipe</option>
        <?php foreach ( $recipeQuery as $recipe ) : setup_postdata($recipeQuery); ?>
            <option id="<?php echo $recipe->ID; ?>" value="<?php echo $recipeTitle; ?>" <?php if($recipeName == $recipeTitle) echo "selected='selected'";?>><?php echo $recipeName; ?></option>
        <?php endforeach; ?>
    </select>
<?php }

1 个答案:

答案 0 :(得分:0)

好的伙计们,我使用以下功能解决了我的问题。

function tfp_recipes_html($post) {

    // Nonce field for some validation
    wp_nonce_field( plugin_basename( __FILE__ ), 'session' );

    global $post;

    $recipeArgs = array(
        'post_type' => 'recipe'
    );
    $recipes = get_posts($recipeArgs);

    $recipeName = get_post_meta( $post->ID, 'recipe_name', true );

    ?>

    <select name="recipe_names" id="recipes">
        <option value="null">Select a Featured Recipe</option>
        <?php foreach ( $recipes as $recipe ) : setup_postdata($recipe); ?>
            <option id="<?php echo $recipe->ID; ?>" value="<?php $recipeTitle = $recipe->post_title; echo $recipeTitle; ?>" <?php if($recipeName == $recipeTitle) echo "selected='selected'";?>><?php echo $recipeTitle; ?></option>
        <?php endforeach; ?>
    </select>
<?php }

虽然现在我还有另一个问题,即保存另一个自定义元数据盒。添加元数据的功能是:

function tfp_tips_html($post) {

    // Nonce field for some validation
    wp_nonce_field( plugin_basename( __FILE__ ), 'session' );

    global $post;

    echo $post->ID;

    $tipArgs = array(
        'post_type' => 'tip'
    );
    $tips = get_posts($tipArgs);

    $tipData = get_post_meta( $post->ID, $tipID, true );

    foreach ( $tips as $tip ) : setup_postdata($tip);

        $tipID = 'tip_' . $tip->ID; ?>

        <label for="<?php echo $tipID; ?>">
            <input type="checkbox" name="<?php echo $tipID; ?>" id="<?php echo $tipID; ?>" value="1" <?php if ($tipData == 1) echo "checked='checked'"; ?> />
            <?php $tipTitle = $tip->post_title; echo $tipTitle; ?>
        </label>
        <br />
    <?php endforeach;
}

与配方功能类似,但这会添加每个具有特定ID的复选框。现在保存它的功能是:

function tfp_save_tips($post_id) {

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

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }

    if ( ! wp_verify_nonce( $_POST['custom_post_type'], plugin_basename(__FILE__) ) ) return;

    global $tipID;

    add_post_meta($post->ID, $tipID, $_POST[$tipID], true) or update_post_meta($post->ID, $tipID, $_POST[$tipID]);
}

问题是它没有保存数据,而是进入空白页面。

<强> [编辑]

好的,我有数据保存,以及当你回到帖子时正确检查复选框。唯一的问题是它发布到一个空白页面,这是不好的。需要刷新编辑器。

现在使用的功能是:

function tfp_save_tips($post_id) {

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

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }

    if ( ! wp_verify_nonce( $_POST['session'], plugin_basename(__FILE__) ) ) return;

    $tipArgs = array(
        'post_type' => 'tip'
    );
    $tips = get_posts($tipArgs);

    foreach ( $tips as $tip ) : setup_postdata($tip);

    $tipID = 'tip_' . $tip->ID;

    endforeach;

    add_post_meta($post_id, $tipID, $_POST[$tipID], true) or update_post_meta($post_id, $tipID, $_POST[$tipID]);
}
相关问题