ACF自定义查询GET URL

时间:2019-03-30 14:10:39

标签: php mysql wordpress advanced-custom-fields

因此,我有几个使用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的字段

2 个答案:

答案 0 :(得分:0)

我不确定您问题的起因,但​​以下几件事可能会有所帮助...

在执行任何操作之前,将$_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;
    } );
?>

然后在存档模板中,您有一个未关闭的标签(<p class="excerpt">)。我已经整理好您的代码,见下文。

<?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; ?>

答案 1 :(得分:0)

因此,基本上在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是我从中获取信息的字段。

希望这对某人有帮助。