【问题标题】:ACF Custom Query GET URLACF 自定义查询 GET URL
【发布时间】:2023-03-20 11:16:02
【问题描述】:

所以,我有几个使用 ACF 的自定义字段。在他们身上,我有一个名为 cd_location 的字段(组 -> 中继器 -> 选择)。

我制作了一个页面模板(基于我的自定义工作存档),它应该返回包含位置 Y (GET / xxx.com/?loc=YYY) 的所有课程,但它根本不起作用,我不这样做不知道为什么。有人可以建议吗?

基于Dynamic $_GET parameters。

我最常用的 php 代码:

global $post;
global $_GET;
$heading = get_field( 'heading', $post->ID );
$course_dates = get_field('course_dates', $post->ID);

然后我有循环:

<?php
                if ( have_posts() ) : 
                    $counter = 0;
                    while ( have_posts() ) : the_post(); ?>
                        <div class="one_half<?php echo ( ++$counter == 2 ) ? ' last_column' : ''; ?>">
                            <div class="break-link">
                                <?php
                                get_portfolio_item_thumbnail( $post->ID, '5', '528', '328', true );
                                ?>
                            </div>
                            <h4 class="cont-subtitles"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
                            <p class="excerpt"><?php echo excerpt(15); ?></p>
                        </div><!-- end one_half -->

                        <?php if ( $counter == 2 ) : ?>
                            <div class='clear'> </div>
                        <?php $counter = 0; 
                        endif;
                    endwhile;
                endif; ?>

这在functions.php上:

add_action( 'pre_get_posts', function( $query ) {
        if ( isset( $query->query_vars[ 'post_type' ] ) && $query->query_vars[ 'post_type' ] == 'e-kursus' && isset( $_GET[ 'loc' ] ) ) {
            $query->set( 'meta_key', 'cd_location' );
            $query->set( 'meta_value', sanitize_text_field( $_GET[ 'loc' ] ) );
        }
        return $query;
    } );

它返回一个带有页面名称的虚拟帖子,没有来自 ACF 的字段

【问题讨论】:

    标签: php mysql wordpress advanced-custom-fields


    【解决方案1】:

    我不确定您的问题的原因,但这里有几件事可能会有所帮助...

    在你做任何事情之前,将 $_GET['loc'] 包装在 WordPress 的 sanitize_text_field() 函数中,这将有助于防止任何脚本注入。请参阅下面的代码示例。

    然后我会在您的主题 functions.php 文件中移动 pre_get_posts 操作。

    functions.php

    <?php
        add_action( 'pre_get_posts', function( $query ) {
            if ( isset( $query->query_vars[ 'post_type' ] ) && $query->query_vars[ 'post_type' ] == 'e-kursus' && isset( $_GET[ 'loc' ] ) ) {
                $query->set( 'meta_key', 'cd_location' );
                $query->set( 'meta_value', sanitize_text_field( $_GET[ 'loc' ] ) );
            }
            return $query;
        } );
    ?>
    

    然后在您的存档模板中,您有一个未闭合的标签 (&lt;p class="excerpt"&gt;)。我已经整理了你的代码,见下文。

    <?php if ( have_posts() ) : ?>
        <?php $i = 0; ?>
        <?php while ( have_posts() ) : the_post(); $i++; ?>
            <div class="one_half<?php echo $i == 2 ? ' last_column' : ''; ?>">
                <div class="break-link">
                    <?php get_portfolio_item_thumbnail( get_the_ID(), '5', '528', '328', true ); ?>
                </div>
                <h4 class="cont-subtitles"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
                <p class="excerpt"><?php echo excerpt(15); ?></p>
            </div>
    
            <?php if ( $i % 2 == 0 ) : ?>
                <div class="clear"></div>
            <?php endif; ?>
        <?php endwhile; ?>
    <?php endif; ?>
    

    【讨论】:

    • 嗨,这仍然不起作用,同样的结果,空帖子。我更新了我的代码
    【解决方案2】:

    所以,基本上在 WP 4.8.3 之后事情就搞砸了。

    解决方案是functions.php:

    function my_posts_where( $where ) {
        global $wpdb;
        $where = str_replace(
                  "meta_key = 'course_dates_%", 
                  "meta_key LIKE 'course_dates_%",
                  $wpdb->remove_placeholder_escape($where)
        );
        return $where;
    }
    add_filter('posts_where', 'my_posts_where');
    

    循环页面:

    $args = array(
                        'numberposts'   => -1,
                        'post_type'     => 'e-kursus',
                        'post_status'   => 'publish',
                        'meta_query'    => array(
                            array(
                                'key'       => 'course_dates_%_cd_location',
                                'value'     => $location,
                                'compare'   => '='
                            )
                        )
                    );
    
                    $the_query = new WP_Query( $args );
    

    基本上course_dates 是转发器,cd_location 是我需要从中获取信息的字段。

    希望这对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2021-08-17
      • 2020-05-22
      • 2019-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多