0

我为具有“最新产品”页面的客户建立了一个 WooCommerce 网站。由于客户行业的竞争性质,客户希望能够选择某些产品不出现在最新产品中。

为此,我使用以下代码在产品编辑器中添加了一个自定义的“从最新产品中隐藏”复选框:

add_action('add_meta_boxes', 'gd_add_product_extra_options_metabox');
function gd_add_product_extra_options_metabox(){
    add_meta_box("product_extra_options_meta", "Extra Options", "gd_product_extra_options_metabox", "product", "side", "high");
}

// ADD CUSTOM FIELDS //
function gd_product_extra_options_metabox(){
global $post;
    // Nonce field to validate form request came from current site
    wp_nonce_field( basename( __FILE__ ), 'product_fields' );
    // Get the field data if it's already been entered
$hide_from_latest = get_post_meta( $post->ID, 'hide_from_latest', true );
if($hide_from_latest == '1'){$checked = ' checked';}else{$checked = '';}
    // Output the field
echo '<p><input type="checkbox" name="hide_from_latest" value="1"' . $checked . '> Hide from Latest Products</p>';
}

等等,保存代码等等。复选框似乎工作得很好。

但是,问题在于编写自定义产品查询以获取未选中此复选框的所有最新产品。

为了测试,我只检查并保存了 1 个产品的“隐藏”选项。我的查询如下:

$products = new WP_Query( array (
'post_type'         => 'product',
'post_status'       => 'publish',
'posts_per_page'    => '100',
'order_by'          => 'date',
'order'             => 'ASC',
'meta_query'        => array(
    array(
        'key'       => 'hide_from_latest',
        'value'     => '1',
        'compare'   => 'NOT LIKE'
    ),
)

这给了我一个“没有结果”的非常空的屏幕。

但是,如果我将 meta_query 更改为:

'meta_query'        => array(
    array(
        'key'       => 'hide_from_latest',
        'value'     => '1',
        'compare'   => 'LIKE'
    ),
)

然后它获取并显示我检查并保存选项的 1 个单一产品。

我尝试了各种其他选项,例如使用“=”和“!=”作为“比较”,我尝试将复选框的值更改为“打开”,然后在 meta_query 中使用“值”=>“打开”等。结果总是一样的。

我哪里做错了?

编辑:

这是我的完整代码:

    // ADD CUSTOM OPTIONS TO WOOCOMMERCE PRODUCTS //
add_action('add_meta_boxes', 'gd_add_product_extra_options_metabox');
function gd_add_product_extra_options_metabox(){
    add_meta_box("product_extra_options_meta", "Extra Options", "gd_product_extra_options_metabox", "product", "side", "high");
}

// ADD CUSTOM FIELDS //
function gd_product_extra_options_metabox(){
global $post;
    // Nonce field to validate form request came from current site
    wp_nonce_field( basename( __FILE__ ), 'notice_fields' );
    // Get the field data if it's already been entered
$hide_from_latest = get_post_meta( $post->ID, 'hide_from_latest', true );
if($hide_from_latest == '1'){$checked = ' checked';}else{$checked = '';}
    // Output the field
echo '<p><input type="checkbox" name="hide_from_latest" value="1"' . $checked . '> Hide from Latest Products</p>';
}

// SAVE METABOX DATA //
function gd_save_product_extra_options_metabox( $post_id, $post ) {
    // Return if the user doesn't have edit permissions.
    if ( ! current_user_can( 'edit_post', $post_id ) ) {
        return $post_id;
    }
    // Verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times.
    if ( !wp_verify_nonce($_POST['notice_fields'], basename(__FILE__)) ) {
        return $post_id;
    }
    // Now that we're authenticated, time to save the data.
    // This sanitizes the data from the field and saves it into an array $product_meta.
    $product_meta['hide_from_latest'] = esc_textarea( $_POST['hide_from_latest'] );
    // Cycle through the $notice_meta array.
    foreach ( $product_meta as $key => $value ) :
        // Don't store custom data twice
        if ( 'revision' === $post->post_type ) {
            return;
        }
        if ( get_post_meta( $post_id, $key, false ) ) {
            // If the custom field already has a value, update it.
            update_post_meta( $post_id, $key, $value );
        } else {
            // If the custom field doesn't have a value, add it.
            add_post_meta( $post_id, $key, $value);
        }
        if ( ! $value ) {
            // Delete the meta key if there's no value
            delete_post_meta( $post_id, $key );
        }
    endforeach;
}
add_action( 'save_post', 'gd_save_product_extra_options_metabox', 1, 2 );




if( ! function_exists('latest_products_censored') ) {

    // Add Shortcode
    function latest_products_censored( $atts ) {
        global $woocommerce_loop;

        // Attributes 
        $atts = shortcode_atts(
            array(
                'columns'   => '4',
                'limit'     => '100'
            ),
            $atts, 'products_test'
        );


        $woocommerce_loop['columns'] = $atts['columns'];

        // The WP_Query
        $products = new WP_Query( array (
            'post_type'         => 'product',
            'post_status'       => 'publish',
            'posts_per_page'    => $atts['limit'],
            'order_by'          => 'date',
            'order'             => 'ASC',
            'meta_query'        => array(
                array(
                    'key'       => 'hide_from_latest',
                    'value'     => '1',
                    'compare'   => 'NOT LIKE'
                ),
            )
        ));

        ob_start();

        if ( $products->have_posts() ) { ?>

            <?php woocommerce_product_loop_start(); ?>

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

                    <?php wc_get_template_part( 'content', 'product' ); ?>

                <?php endwhile; // end of the loop. ?>

            <?php woocommerce_product_loop_end(); ?>

            <?php
        } else {
            do_action( "woocommerce_shortcode_products_loop_no_results", $atts     );
            echo "<p>There is no results.</p>";
        }

        woocommerce_reset_loop();
        wp_reset_postdata();

        return '<div class="woocommerce columns-' . $atts['columns'] . '">' . ob_get_clean() . '</div>';
    }

    add_shortcode( 'latest_products_censored', 'latest_products_censored' );
}
4

1 回答 1

0

在对 Wordpress Codex 进行了更多挖掘之后,我找到了答案!

meta_query 需要是:

'meta_query'        => array(
    array(
        'key'       => 'hide_from_latest',
        'value'     => '1',
        'compare'   => 'NOT EXISTS'
    ),
)

我刚刚试了一下,它现在运行良好!:-)

于 2019-09-17T00:55:29.133 回答