在参数中对关系使用META_QUERY

时间:2022-02-15 作者:Matteo Feduzi

我找到了这本指南,我正在遵循它:https://rudrastyh.com/wordpress/meta_query.html

但我发现它不适用于高级自定义字段的关系。为什么?

这是我的代码:

$args=array(
        \'post_type\' => \'post\',
        \'post_status\' => \'publish\',
        \'posts_per_page\' => -1,
        \'meta_key\'          => \'rtp_value\',
        \'orderby\'           => \'meta_value meta_value_num\',
        \'order\' => \'DESC\',
        \'cat\' => 16,
        \'meta_query\' => array(
        \'relation\'  => \'OR\',
        array(
            \'key\'     => \'software_relationship\',
            \'value\'   => array(\'15813\', \'16013\'),
            \'compare\' => \'IN\',
        ),
        ),
    );
有可能吗?

UPDATE:

我继续,但现在我有一个新问题,如何处理简单ACF文本的meta\\u查询?

环顾四周,我发现需要这样做:https://www.advancedcustomfields.com/resources/query-posts-custom-fields/

但这对我来说并不适用="E;和;“如”;操作人员

这是我的代码:

$tema_array_args = array();
$i = 0;
foreach ($tema_array as $tema_array_key) {
    $tema_array_args[$i] = array(\'key\' => \'tema_slot_machine\', \'value\' => \'"\' . $tema_array_key . \'"\', \'compare\' => \'LIKE\');
    $i++;
}

$software_array_args = array();
$i = 0;
foreach ($software_array as $software_array_key) {
    $software_array_key_id = get_post_id_by_slug(slugify($software_array_key));
    $software_array_args[$i] = array(\'key\' => \'software_relazione_slot_machine\', \'value\' => \'"\' . $software_array_key_id . \'"\', \'compare\' => \'LIKE\');
    $i++;
}

$volatilita_array_args = array();
$i = 0;
foreach ($volatilita_array as $volatilita_array_key) {
    $volatilita_array_args[$i] = array(\'key\' => \'volatilita_slot_machine\', \'value\' => "\'" . $volatilita_array_key . "\'", \'compare\' => \'=\');
    $i++;
}

if($filtro_applicato == \'RTP più alto\') {

    if($tema == \'\' && $software == \'\' && $volatilita == \'\') {

        $args=array(
            \'post_type\' => \'post\',
            \'post_status\' => \'publish\',
            \'posts_per_page\' => -1,
            \'meta_key\'          => \'rtp_slot_machine\',
            \'orderby\'           => \'meta_value meta_value_num\',
            //\'post__not_in\'    => array(91690), // esclude l\'articolo "inizia qui"
            \'order\' => \'DESC\',
            \'paged\' => $paged,
            \'cat\' => 16,
        );

    } elseif($tema != \'\' || $software != \'\' || $volatilita != \'\') {

        if($tema != \'\' && $software == \'\' && $volatilita == \'\') {
            $relazione = \'OR\';
        } elseif($tema == \'\' && $software != \'\' && $volatilita == \'\') {
            $relazione = \'OR\';
        } elseif($tema != \'\' && $software == \'\' && $volatilita != \'\') {
            $relazione = \'OR\';
        } else {
            $relazione = \'AND\';
        }

        $args=array(
            \'post_type\' => \'post\',
            \'post_status\' => \'publish\',
            \'posts_per_page\' => -1,
            \'meta_key\'          => \'rtp_slot_machine\',
            \'orderby\'           => \'meta_value meta_value_num\',
            //\'post__not_in\'    => array(91690), // esclude l\'articolo "inizia qui"
            \'order\' => \'DESC\',
            \'paged\' => $paged,
            \'cat\' => 16,
            \'meta_query\' => array(
                \'relation\' => $relazione,
                array(
                \'relation\' => \'OR\',
                    $tema_array_args[0], $tema_array_args[1], $tema_array_args[2], $tema_array_args[3], $tema_array_args[4],
                    $tema_array_args[5], $tema_array_args[6], $tema_array_args[7], $tema_array_args[8], $tema_array_args[9],
                    $tema_array_args[10], $tema_array_args[11], $tema_array_args[12], $tema_array_args[13], $tema_array_args[14],
                    $tema_array_args[15], $tema_array_args[16], $tema_array_args[17], $tema_array_args[18], $tema_array_args[19],
                    $tema_array_args[20], $tema_array_args[21], $tema_array_args[22], $tema_array_args[23], $tema_array_args[24],
                    $tema_array_args[25], $tema_array_args[26], $tema_array_args[27], $tema_array_args[28], $tema_array_args[29],
                    $tema_array_args[30], $tema_array_args[31], $tema_array_args[32], $tema_array_args[33], $tema_array_args[34],
                    $tema_array_args[35], $tema_array_args[36], $tema_array_args[37], $tema_array_args[38], $tema_array_args[39],
                ),
                array(
                    \'relation\' => \'OR\',
                    $software_array_args[0], $software_array_args[1], $software_array_args[2], $software_array_args[3], $software_array_args[4],
                    $software_array_args[5], $software_array_args[6], $software_array_args[7], $software_array_args[8], $software_array_args[9],
                    $software_array_args[10], $software_array_args[11], $software_array_args[12], $software_array_args[13], $software_array_args[14],
                    $software_array_args[15], $software_array_args[16], $software_array_args[17], $software_array_args[18], $software_array_args[19],
                    $software_array_args[20], $software_array_args[21], $software_array_args[22], $software_array_args[23], $software_array_args[24],
                    $software_array_args[25], $software_array_args[26], $software_array_args[27], $software_array_args[28], $software_array_args[29],
                    $software_array_args[30], $software_array_args[31], $software_array_args[32], $software_array_args[33], $software_array_args[34],
                    $software_array_args[35], $software_array_args[36], $software_array_args[37], $software_array_args[38], $software_array_args[39],
                ),
                array(
                    \'relation\' => \'OR\',
                    $volatilita_array_args[0], $volatilita_array_args[1], $volatilita_array_args[2], $volatilita_array_args[3], $volatilita_array_args[4],
                ),
            ),
        );

    }
我做错了什么?

1 个回复
SO网友:Matteo Feduzi

我终于自己解决了,这是我使用的代码:

if($tema == \'\' && $software == \'\' && $volatilita == \'\') {

    $args=array(
        \'post_type\' => \'post\',
        \'post_status\' => \'publish\',
        \'posts_per_page\' => -1,
        \'orderby\'           => \'meta_value meta_value_num\',
        \'order\' => \'DESC\',
        \'paged\' => $paged,
        \'cat\' => $categoria_slot_machine,
    );

} elseif($tema != \'\' || $software != \'\' || $volatilita != \'\') {

    $args=array(
        \'post_type\' => \'post\',
        \'post_status\' => \'publish\',
        \'posts_per_page\' => -1,
        \'orderby\'           => \'meta_value meta_value_num\',
        \'order\' => \'DESC\',
        \'paged\' => $paged,
        \'cat\' => $categoria_slot_machine,
        \'meta_query\' => array(
            \'relation\' => \'AND\',
            array(
                \'relation\' => \'OR\',
            ),
            array(
                \'relation\' => \'OR\',
            ),
            array(
                \'relation\' => \'OR\',
            ),
        )
    );

    $i = 0;
    if ($tema != \'\') {
        foreach ($tema_array as $tema_array_key) {
            $args[\'meta_query\'][0][$i] = array(\'key\' => \'tema_slot_machine\', \'value\' => \'"\' . $tema_array_key . \'"\', \'compare\' => \'LIKE\');
            $i++;
        }
    }
    if ($software != \'\') {
        foreach ($software_array as $software_array_key) {
            $software_array_key_id = get_post_id_by_slug(slugify($software_array_key));
            $args[\'meta_query\'][1][$i] = array(\'key\' => \'software_relazione_slot_machine\', \'value\' => \'"\' . $software_array_key_id . \'"\', \'compare\' => \'LIKE\');
            $i++;
        }
    }

    if ($volatilita != \'\') {
        foreach ($volatilita_array as $volatilita_array_key) {
            echo $volatilita_array_key;
            $args[\'meta_query\'][2][$i] = array(\'key\' => \'volatilita_slot_machine\', \'value\' => $volatilita_array_key, \'compare\' => \'=\');
            $i++;
        }
    }
}

if($filtro_applicato == \'RTP più alto\') {

    $args[\'meta_key\'] = \'rtp_slot_machine\';

} elseif($filtro_applicato == \'Più votate\') {

    $args[\'meta_key\'] = \'_kksr_ratings\';

} elseif($filtro_applicato == \'Nuove slot\') {

    $args[\'order\'] = \'date\';

}
我发帖是因为我希望它能帮助别人:)

相关推荐