带有变量值的ACF查询

时间:2017-10-20 13:06:30

标签: php mysql wordpress advanced-custom-fields

我尝试使用变量执行请求以显示事件的所有采访。 所以在我的查询中,我要求所有帖子“evenement_associe”,即页面的id。但我不知道为什么,查询是空的。 图像是文章的组字段。enter image description here

<?php
            // args
            $id_eventz = get_the_ID();
            $args = array(
                'post_type' => 'post',
                'meta_key' => 'evenement_associe',
                'meta_value' => $id_eventz
            );


            // query
            $the_query = new WP_Query($args);

            ?>

            <?php
            if ($the_query) {
                ?>
                <?php if ($the_query->have_posts()): ?>
                    <?php while ($the_query->have_posts()) : $the_query->the_post() ?>
                        <?php
                        $value_link=get_field('evenement_associe');
                        $id_article=get_the_ID();
                        $title_article=get_the_title($id_article);
                        ?>

                    <ul>
                        <li>
                            <?php

                            ?>
                        </li>
                    </ul>

                    <?php endwhile; ?>
                <?php else: echo 'Aucun article encore réalisé pour cet évènement.'?>

                <?php endif; ?>

                <?php wp_reset_query();     // Restore global post data stomped by the_post().
                //
            } ?>

2 个答案:

答案 0 :(得分:0)

如果您不知道出了什么问题,可以使用以下方法检查SQL查询:

var_dump($the_query->request);

您确定有任何关于元evenement_associe的帖子,其值为$id_eventz吗?通过查询签入数据库:

SELECT post.* FROM wp_posts AS post
LEFT JOIN wp_postmeta AS meta
ON post.ID = meta.post_id
WHERE meta.meta_key = 'evenement_associe' AND meta.meta_value = ID_THAT_YOU_LOOKING_FOR

答案 1 :(得分:0)

我想要这个值enter image description here