Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Wordpress WP_查询仅比较元查询键的月份部分_Php_Sql_Wordpress - Fatal编程技术网

Php Wordpress WP_查询仅比较元查询键的月份部分

Php Wordpress WP_查询仅比较元查询键的月份部分,php,sql,wordpress,Php,Sql,Wordpress,我一直在尝试使用wordpress wp_查询方法,但我的案例是具体的 我需要选择当月的所有产品,所以这里是我的代码 $args = array( 'numberposts' => -1, 'post_type' => 'product', 'meta_query' => array( 'Relation' => 'AND', array( 'key' => 'wpcf-pro

我一直在尝试使用wordpress wp_查询方法,但我的案例是具体的

我需要选择当月的所有产品,所以这里是我的代码

$args = array(
    'numberposts' => -1,
    'post_type' => 'product',

    'meta_query' => array(
            'Relation' => 'AND',
    array(
            'key'     => 'wpcf-product_status',
            'value'   => 'valid',
            'compare' => '='
        ),
    array(
            'key'     => 'wpcf-product-start_date',
            'compare' => '=',

            'date_query' => array(
                array(
                        'month'      => date('m'),
                        'compare'   => '=',
                ),              
        ),
    ),
    ),
);
// get results
$the_query = new WP_Query( $args );
我不能得到确切的结果。 如何才能像SQL那样只测试日期月份(
WHERE MONTH(wpcf-product-start_date)=MONTH(NOW())


有什么想法吗?

试试这个。并确保“wpcf-product-start_date”的格式与“date('m')”的格式相同:)

以下是几个附加选项:
  • 如果要过滤给定年份中的给定月份,只需计算该月份的时间戳周期,并使用元查询的相应比较运算符进行过滤

  • >p>您可能需要考虑更改您的代码的格式:>代码> WPCFPROST-STARTHORDATION< <代码>自定义字段值,<>代码> Y-MD < /COD>,<代码> Y-M< <代码>或<代码> M>代码>,根据您的需要.

  • 您可以使用
    {added,updated,deleted}\u post_meta
    钩子(参见示例)自动保存一个新的自定义字段,其中包含根据
    wpcf-product-start_日期
    时间戳计算的月数。例如:

    add_action( 'added_post_meta',   'b2e_update_post_meta', 10, 4 );
    add_action( 'updated_post_meta', 'b2e_update_post_meta', 10, 4 );
    
    function b2e_update_post_meta( $meta_id, $post_id, $meta_key, $meta_value )
    {
        if ( 'wpcf-product-start-date' === $meta_key )
        {
            // avoid infinite loop
           remove_action( current_filter(), __FUNCTION__ );            
    
           // add/update the corresponding month value
           update_post_meta( $post_id, 
                             'wpcf-product-start-month', 
                             date( 'n', $meta_value ) 
           );
        }
    }
    
    add_action( 'deleted_post_meta', 'b2e_delete_post_meta', 10, 4 );
    
    function b2e_delete_post_meta( $deleted_meta_ids, $post_id, $meta_key,
                                   $only_delete_these_meta_values )
    {
        if ( 'wpcf-product-start-date' === $meta_key )
        {
            // avoid infinite loop
            remove_action( current_filter(), __FUNCTION__ );            
    
           // delete the corresponding month key        
            delete_post_meta( $post_id, 'wpcf-product-start-month' );
        }
    }
    
    然后,您可以在元查询中使用
    wpcf产品开始月份

    注意,我在这里使用了
    wpcf产品开始日期
    。不知何故,这并没有为带有meta键的自定义字段触发
    wpcf-product-start\u date
    。但是如果你对此有问题,请告诉我

  • 如果你喜欢冒险,你可以使用
    posts\u,其中
    过滤器:

    $args = array(
        'posts_per_page' => -1,
        'post_type'      => 'product',
        'meta_query'     => array(
            'relation'    => 'AND',
            array(
                'key'     => 'wpcf-product_status',
                'value'   => 'valid',
                'compare' => '='
            ),
            array(
                'key'     => 'wpcf-product-start_date',
                'compare' => '=',
                'value'   => 'replace_me',
            ),
        ),
    );
    
    add_filter( 'posts_where', 'b2e_posts_where' );
    $the_query = new WP_Query( $args );
    remove_filter( 'posts_where', 'b2e_posts_where' );
    
    在哪里

    请注意,这假定
    replace\u me
    值位于第二个元查询数组项中。
    如果对数组项重新排序(例如,
    wp\u postemta、mt1、
    ),表名可能会更改。有可能找到 自动更正表名,但这超出了此答案的范围

  • 希望这有帮助。

    或者

    您可以将月开始和月结束作为时间戳,并使用“中间”元比较:

    例如

    或者

    $args=array(
    “numberposts”=>-1,
    “post_类型”=>“产品”,
    “元查询”=>数组(
    '关系'=>'和',
    排列(
    '键'=>'wpcf-product-start_date',
    “值”=>$month\u开始,
    '比较'=>'>='
    ),
    排列(
    '键'=>'wpcf-product-start_date',
    “值”=>$month\u end,
    
    “比较”=>“您的
    wpcf-product-start\u date
    自定义字段的格式是什么?您不能在自定义字段上使用
    date\u query
    。格式是timestamp。您可以将自定义字段保存为另一种格式吗?您是否正在查找给定年份中的给定月份(Y-m)?如果是这种情况,那么我们可以找到给定Y-m的时间戳周期。实际上,wpcf-product-start_date保存为时间戳。这会使事情变得复杂。时间戳中的月份是否始终与产品发布的月份相同?如果是,我认为最好的解决方案是使用wp_qyery获取所有产品,然后在循环中使用php进行过滤
    $args = array(
        'posts_per_page' => -1,
        'post_type'      => 'product',
        'meta_query'     => array(
            'relation'    => 'AND',
            array(
                'key'     => 'wpcf-product_status',
                'value'   => 'valid',
                'compare' => '='
            ),
            array(
                'key'     => 'wpcf-product-start_date',
                'compare' => '=',
                'value'   => 'replace_me',
            ),
        ),
    );
    
    add_filter( 'posts_where', 'b2e_posts_where' );
    $the_query = new WP_Query( $args );
    remove_filter( 'posts_where', 'b2e_posts_where' );
    
    function b2e_posts_where( $where )
    {
        $month = 3; // Edit this value
    
        if( FALSE !== stripos( $where, 'replace_me' ) )
        {
            $from  = "CAST(mt1.meta_value AS CHAR) = 'replace_me'";
            $to    = "MONTH( FROM_UNIXTIME( 1 * mt1.meta_value ) ) = $month 
                      AND YEAR( FROM_UNIXTIME( 1 * mt1.meta_value ) ) > 1970 ";
            $where = str_ireplace( $from, $to, $where );
        }
        return $where;
    }
    
    $year = date('Y'); $month = date('m')
    $days_in_month = cal_days_in_month(CAL_GREGORIAN, $month, $year);
    $month_start = strtotime($year .  '-' . $month . '-1 00:00:00');
    $month_end = strtotime($year .  '-' . $month . '-' . $days_in_month . ' 23:59:59');
    
    $args = array(
        'numberposts' => -1,
        'post_type' => 'product',
        'meta_query' => array(
            'key' => 'wpcf-product-start_date',
            'value' => array(
                 $month_start,
                 $month_end
            ),
            'compare' => 'BETWEEN'
        )
    );
    
    $args = array(
        'numberposts' => -1,
        'post_type' => 'product',
        'meta_query' => array (
            'relation' => 'AND',
            array(
                'key' => 'wpcf-product-start_date',
                'value' => $month_start,
                'compare' => '>='
            ),
            array(
                'key' => 'wpcf-product-start_date',
                'value' => $month_end,
                'compare' => '<='
            )
        )
    )