另一个自定义帖子类型下的自定义帖子类型列表

时间:2018-02-02 06:43:48

标签: php wordpress dropdown

我有一个名为“产品”的自定义帖子类型,它有一个下拉元框选项,用于另一个名为在线商店的自定义帖子类型(例如,商店1,商店2,商店3等)。我能够成功地将元数据键和值保存到数据库中。

在single-onlineshops.php上,我有一个产品库的一部分。是否可以在单个帖子模板下创建产品循环列表?

目前这是我的功能片段:

        function MSB_custom_meta_box() {
        //another add_meta_box here
        add_meta_box("featured-products", "Featured Products", "featured_product_metabox", "product", "normal", "high", null);
    }
    add_action("add_meta_boxes", "MSB_custom_meta_box");

    save_featured_product_metabox ($post_id, $post, $update) {
        if ( isset( $_POST['online_shop_select'] ) ) { // Input var okay.
        update_post_meta( $post_id, 'online_shop_select', sanitize_text_field( wp_unslash( $_POST['online_shop_select'] ) ) ); //
    Input var okay. }
    } 
    add_action ("save_post", "save_featured_product_metabox", 10, 3); 

    function featured_product_metabox ($post) { 
        wp_nonce_field(basename(__FILE__), "featured-product-nonce"); ?>

        <label for="online_shop_select">Select Seed Bank</label>
        <?php 
            $meta = get_post_meta( $post->ID);
            $online_shop_select = ( isset( $meta['online_shop_select'][0] ) && '' !== $meta['online_shop_select'][0] ) ? $meta['online_shop_select'][0] : '';
            $args_pages = array(
                'post_type'             => 'online-shops',
                'depth'                 => 0,
                'child_of'              => 0,
                'selected'              => $online_shop_select,
                'echo'                  => 1,
                'name'                  => 'online_shop_select',
                'id'                    => 'online_shop_select',
                'class'                 => null,
                'show_option_none'      => null,
                'show_option_no_change' => null,
                'option_none_value'     => esc_html__( '&ndash; Select &ndash;', THEME_SLUG ),
            ); 
            wp_dropdown_pages($args_pages); ?>
    <br>
    <?php }

非常感谢任何帮助或建议。非常感谢提前。

1 个答案:

答案 0 :(得分:0)

我想通过这段代码解决它:

    $meta_value = $post->ID;
                    $args = array (
                        'post_type' => 'product',
                        'meta_key' => 'online_shop_select',
                        'meta_value' => $meta_value
                    );
相关问题