Wordpress 按自定义字段筛选搜索结果

Wordpress 按自定义字段筛选搜索结果,wordpress,Wordpress,我想按自定义字段筛选搜索结果 我有一个自定义字段cp_city,我希望用户按城市过滤结果,因此我在搜索框旁边添加了一个城市下拉列表,并更改查询以更改结果,但由于某些原因,它无法工作 这是我试过的 <?php $city = isset($_GET['city']) ? trim($_GET['city']) : ''; $s = $_GET['s']; $paged = ( get_query_var('paged'

我想按自定义字段筛选搜索结果

我有一个自定义字段cp_city,我希望用户按城市过滤结果,因此我在搜索框旁边添加了一个城市下拉列表,并更改查询以更改结果,但由于某些原因,它无法工作

这是我试过的

            <?php
        $city = isset($_GET['city']) ? trim($_GET['city']) : '';
        $s = $_GET['s'];

        $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
        query_posts( array('s' => $s, 'scat' => $scat, 'post_type' => 'ads', 'ignore_sticky_posts' => 1, 'meta_key' => 'cp_state', 'meta_value' => $city, 'meta_compare' => 'LIKE', 'paged' => $paged, 'orderby' => 'rand') );
        ?>

但两次尝试都失败了。任何人都能想出正确的解决方案。

你应该这样做:

<?php
$city = isset($_GET['city']) ? trim($_GET['city']) : '';
$s = $_GET['s'];

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
query_posts( 
    array(
        's' => $s, 
        'scat' => $scat, 
        'post_type' => 'ads', 
        'ignore_sticky_posts' => 1, 
        'meta_query' = > array(
            'key' => 'cp_city', 
            'value' => $city, 
            'compare' => 'LIKE' 
        ),
        'paged' => $paged, 
        'orderby' => 'rand') 
    );

您应该这样做:

<?php
$city = isset($_GET['city']) ? trim($_GET['city']) : '';
$s = $_GET['s'];

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
query_posts( 
    array(
        's' => $s, 
        'scat' => $scat, 
        'post_type' => 'ads', 
        'ignore_sticky_posts' => 1, 
        'meta_query' = > array(
            'key' => 'cp_city', 
            'value' => $city, 
            'compare' => 'LIKE' 
        ),
        'paged' => $paged, 
        'orderby' => 'rand') 
    );

在您的第二个代码中-元密钥是“cp_state”或“cp_city”?我的元密钥是cp_state。在您的第二个代码中-元密钥是“cp_state”或“cp_city”?我的元密钥是cp_state。