Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.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 在WP_查询中将字符串作为数字进行比较_Php_Wordpress_Comparison - Fatal编程技术网

Php 在WP_查询中将字符串作为数字进行比较

Php 在WP_查询中将字符串作为数字进行比较,php,wordpress,comparison,Php,Wordpress,Comparison,我正在对一个名为“task”的自定义帖子类型执行WP_查询,该类型有一个字段“stage”。我需要用另一个任务的“阶段”>=获取所有帖子。问题是stage是一个字符串值,所以在元查询中使用>=运算符将不起作用 目前我的方法是创建一个数组,其中包含从$stage到50的字符串值,并查询该数组中谁的stage。这将暂时起作用,因为没有阶段超过12,但不是一个非常可扩展或动态的解决方案。我想知道是否有更好的方法在对WP_查询中接收的值使用运算符之前对其进行变异 $stage = get_fie

我正在对一个名为“task”的自定义帖子类型执行WP_查询,该类型有一个字段“stage”。我需要用另一个任务的“阶段”>=获取所有帖子。问题是stage是一个字符串值,所以在元查询中使用>=运算符将不起作用

目前我的方法是创建一个数组,其中包含从$stage到50的字符串值,并查询该数组中谁的stage。这将暂时起作用,因为没有阶段超过12,但不是一个非常可扩展或动态的解决方案。我想知道是否有更好的方法在对WP_查询中接收的值使用运算符之前对其进行变异

    $stage = get_field('stage', $task_id);

    $future_tasks = array();
    for ($i = intval($stage); $i <= intval($stage) + 50; $i++) {
        array_push($future_tasks, strval($i));
    }

    $tasks_query_args = array(
        'post_type' => array('task'),
        'posts_per_page' => -1,
        'order' => 'DESC',
        'meta_query' => array(
            '0' => array(
                'key' => 'project',
                'value' => $project_id,
                'compare' => '=',
            ),
            '1' => array(
                'key' => 'stage',
                'value' => $future_tasks,
                'compare' => 'IN',
            ),
            'relation' => 'AND',
        ),
    );
但这不起作用,因为stage字段的值是字符串


想知道如何更好地实现这一点有什么想法吗?

似乎,使用
numeric
类型可以解决问题:

$tasks_query_args = array(
    'post_type'      => array( 'task' ),
    'posts_per_page' => - 1,
    'order'          => 'DESC',
    'meta_query'     => array(
        '0'        => array(
            'key'     => 'project',
            'value'   => intval( $project_id ),
            'compare' => '=',
            'type'    => 'numeric',
        ),
        '1'        => array(
            'key'     => 'stage',
            'value'   => intval( $task_id ),
            'compare' => '>=',
            'type'    => 'numeric',
        ),
        'relation' => 'AND',
    ),
);
$tasks_query_args = array(
    'post_type'      => array( 'task' ),
    'posts_per_page' => - 1,
    'order'          => 'DESC',
    'meta_query'     => array(
        '0'        => array(
            'key'     => 'project',
            'value'   => intval( $project_id ),
            'compare' => '=',
            'type'    => 'numeric',
        ),
        '1'        => array(
            'key'     => 'stage',
            'value'   => intval( $task_id ),
            'compare' => '>=',
            'type'    => 'numeric',
        ),
        'relation' => 'AND',
    ),
);