按自定义域过滤自定义帖子(ACF)

时间:2015-04-26 作者:bypatryk

我在一个有摄影比赛的网站上工作。每场比赛都有很多额外的信息,我将这些信息存储在自定义字段中(感谢高级自定义字段)。现在我想搜索比赛,根据一些自定义字段值过滤它们。

我的function.php 文件如下所示:

// Custom Post Search
add_action(\'pre_get_posts\', \'my_pre_get_posts\');

function my_pre_get_posts( $query )
{
    // validate
    if( is_admin() )
    {
        return;
    }

    if( !$query->is_main_query() )
    {
        return;
    }

    if( !empty($_GET[\'fee\']) )
    {
        $contestFee = explode(\',\', $_GET[\'fee\']);

        $contestFeeArray = array(
            \'key\'       => \'fee\',
            \'value\'     => $contestFee,
            \'compare\'   => \'IN\',
        );
    }
    if( !empty($_GET[\'theme\']) )
    {
        $contestTheme = explode(\',\', $_GET[\'theme\']);

        $contestThemeArray = array(
            \'key\'       => \'theme\',
            \'value\'     => $contestTheme,
            \'compare\'   => \'IN\',
        );
    }

    $meta_query[] = array(
        \'relation\' => \'OR\',
        $contestThemeArray,
        $contestFeeArray,
    );

    $query->set(\'meta_query\', $meta_query);

    return;

}
?>
现在我像这样查询我的搜索website.com/contests/?fee=free&theme=landscape 这就是问题所在。如果我只查询fee 珍视一切完美的运作。但当我想询问theme 值不显示任何内容。

我还粘贴了通过get_field_object (这是高级自定义字段的功能):

fee 自定义字段

Array ( [ID] => 136 [key] => field_553bee3a1f8ff [label] => Is contest paid or free? [name] => fee [prefix] => [type] => radio [value] => free [menu_order] => 7 [instructions] => [required] => 1 [id] => [class] => [conditional_logic] => 0 [parent] => 8 [wrapper] => Array ( [width] => [class] => [id] => ) [_name] => fee [_input] => [_valid] => 1 [choices] => Array ( [free] => Free [paid] => Paid ) [other_choice] => 0 [save_other_choice] => 0 [default_value] => free [layout] => vertical )
theme 自定义字段

Array ( [ID] => 23 [key] => field_5523fed672b4b [label] => Theme [name] => theme [prefix] => [type] => select [value] => Array ( [0] => nude [1] => people [2] => fine-art ) [menu_order] => 12 [instructions] => [required] => 1 [id] => [class] => [conditional_logic] => 0 [parent] => 8 [wrapper] => Array ( [width] => [class] => [id] => ) [_name] => theme [_input] => [_valid] => 1 [choices] => Array ( [aerial] => Aerial [fine-art] => Fine Art [landscape] => Landscape [nature] => Nature [nude] => Nude [people] => People [wildlife] => Wildlife ) [default_value] => Array ( [] => ) [allow_null] => 0 [multiple] => 1 [ui] => 1 [ajax] => 1 [placeholder] => [disabled] => 0 [readonly] => 0 )
So to wrap it up - 我想查询自定义帖子(竞赛)搜索website.com/contests/?fee=free&theme=landscape 在这种情况下,展示比赛是免费的,有景观主题。

我无法找出我的代码出了什么问题。你对这项工作有什么想法吗?

谢谢

1 个回复
最合适的回答,由SO网友:bypatryk 整理而成

好的,在深入研究之后,我发现一些由ACF创建的自定义字段的值存储在MySQL数据库中,并包含一些附加信息,如a:5:{i:0;s:6:"Aerial";i:1;s:6:"Nature";i:2;s:4:"Nude";i:3;s:6:"People";i:4;s:8:‌​"Wildlife";}. 当自定义字段可以存储多个值(如复选框)时,就会发生这种情况。

因此,我编辑了我的部分查询:if(!empty($\\u GET[\'theme]){$contenttheme=explode(\',\',$\\u GET[\'theme]);

    $contestThemeArray = array(
        \'key\'       => \'theme\',
        \'value\'     => $contestTheme,
        \'compare\'   => \'IN\',
    );
}
对此:

if( !empty($_GET[\'contest_theme\']) )
{
    $contestTheme = explode(\',\', $_GET[\'contest_theme\']);
    $contestThemeArray = array(\'relation\' => \'OR\');
    foreach ($contestTheme as $theme) {
        $contestThemeArray[] = array(
            \'key\'       => \'contest_theme\',
            \'value\'     => $theme,
            \'compare\'   => \'LIKE\',
        );
    }

}

结束

相关推荐

Add get_option to jquery

我创建了一个设置页面,其中包含自定义插件的各种单选按钮选项。我创建的一个单选按钮是用值yes或no“显示点”。除了下面的jQuery函数外,我可以在任何其他时间使用该值。我想获得下面jQuery函数中用于“dots”的选项的值。jQuery脚本<script type=\"text/javascript\"> jQuery(document).ready(function(){ jQuery(\'.single-item\').slick({ arrows: