WP_query中继器“不喜欢”无法正常工作

时间:2018-07-12 06:40:23

标签: php wordpress repeater

我对WP_query和Repeater有一个非常烦人的问题。如果$ _GET不等于数据库中的值,我会尝试显示一些内容。但是当compare => NOT LIKE时,它现在可以正常工作。 这是我的代码:

function wyszukiwanie_trasy()
{


    echo '<form action="" method="get">';
    echo '<input type="text" name="od" placeholder="Stacja początkowa">';
    echo '<input type="text" name="do" placeholder="Stacja końcowa">';
    echo '<input type="date" name="data">';
    echo '<input type="submit" value="Wyszukaj">';
    echo '</form>';
    $loop = new WP_Query(array('meta_query' => array(
        'relation' => 'AND',
        array(

            'key' => 'date_%_daty_wykluczone',
            'value'   => str_replace("-","",$_GET['data'],$i),
            'compare' => 'NOT LIKE'),

        ),
        array(

            'value'   => $_GET['od'],

        ),
        array(
            'value'   => $_GET['do'],

    ),'post_type' => 'trasy', 'posts_per_page' => -1));
    while ($loop->have_posts()) : $loop->the_post();
        $id = get_the_ID();


        if($_GET['od'] && $_GET['do'] && $_GET['data'] && $_GET['od']!=$_GET['do'])
        {
            if( have_rows('location') ):

                // loop through the rows of data
                while ( have_rows('location') ) : the_row();

                    // display a sub field value
                    ?>
                    <span style="font-weight: bold"><?php the_sub_field('lokacja');?></span>
                    <?php
                    echo ' ';
                endwhile;

                echo '<br />';
            else :

                echo 'Brak połączeń';

            endif;

        }
        echo $loop->request;

endwhile; wp_reset_query();

    }

这是我的SQL请求:

SELECT wp_posts.* FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND ( ( wp_postmeta.meta_key LIKE 'date_%_daty_wykluczone' AND wp_postmeta.meta_value LIKE '%20180715%' ) ) AND wp_posts.post_type = 'trasy' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'acf-disabled' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC 

Imo是'key'=>'date _%_ daty_wykluczone'的问题,在我的数据库中,元密钥如下所示:date_0_daty_wykluczone date_1_daty_wykluczone,当我将这个'key'=>'date _%_ daty_wykluczone'更改为' key'=>'date_1_daty_wykluczone','NOT LIKE'正常工作

预先感谢:)

1 个答案:

答案 0 :(得分:0)

尝试此代码结构,

使用元查询的“关系”,可以选择多个元关键字。您可以将“比较”更改为该功能可用的任何操作。 (来自食典)比较(字符串)-要测试的运算符。可能的值为'=','!=','>','> =','<','<=','LIKE','NOT LIKE','IN','NOT IN','BETWEEN ”,“不存在”,“存在”和“不存在”。默认值为'='。

// WP_Query arguments
    $args = array (
            'post_type'          => 'your-post-type',
                'meta_query'          => array(
                'relation' => 'AND', 
             array(
                 'key'       => 'color',
                 'value'     => 'Red',
                 'compare'   => 'LIKE',
                    ),

             array(
                 'key' => 'color',
                 'value' => 'Plain',
                 'compare' => 'Like'
                    ),
                 ), 
             );

// The Query
$query = new WP_Query( $args );

// The Loop
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        // do something
    }
} else {
    // no posts found
}

// Restore original Post Data
wp_reset_postdata();

元查询比较运算符说明

关于您期望的前几项工作:

=   equals
!=  does not equal
>   greater than
>=  greater than or equal to
<   less than
<=  less than or equal to

Like NOT LIKE 是SQL运算符,可用于添加通配符,因此您可以使用如下所示的元查询:

array( 
    'key' => 'name', 
    'value' => 'Pat', 
    'compare' => 'LIKE'
)

这将返回元值“ name”具有字符串“ Pat”的所有帖子。在这种情况下,“ Pat”,“ Patricia”和“ Patrick”将全部退还给您。有非WordPress教程说明here