首页 > 解决方案 > 按自定义字段中的部分字符串查询

问题描述

我正在尝试查询是否存在于自定义字段中的字符串。

例如,在自定义字段中,我有一个值,例如:如果我有一个来自Milano romani的字符串MilanoGET,我应该找到它。但是即使自定义字段中没有字符串匹配,以下内容也会给我所有帖子

<?php
    $myTerm = $_GET['cityName']; 

    $catIds = array();

    $args = get_posts( 
        array( 
            'post_type'      => 'post', 
            'posts_per_page' => -1, 
            'meta_query'     => array(
                array(
                    'key'     => 'usp-custom-23',
                    'value'   => $myTerm,
                    'compare' => 'LIKE'
                )
            ) 
        ) 
    );
    $query = new WP_Query( $args );
    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            $query->the_post();
            $id = get_the_ID();
            array_push($catIds, $id);
        }
        $catIds = implode( ", ", $catIds );

        if(count($catIds) > 0){
            $arrayFUllCity = "fullCity";
        } else {
            $arrayFUllCity = "empty";
        }
    }
        var_dump($catIds);
?>

标签: phpwordpress

解决方案


推荐阅读