0
votes

So, I have a couple custom fields using ACF. On them I have a field called cd_location (group -> repeater -> select).

I've made a page template (based on my custom working archive) that should return ALL the courses that contain the location Y (GET / xxx.com/?loc=YYY) but it's not working at all and I don't know why. Can someone advise please?

Based on Dynamic $_GET parameters.

My top php code:

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

Then I have the loop:

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

This on 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;
    } );

It's returning a dummy post with the name of the page with no fields from ACF

2

2 Answers

0
votes

I am unsure of the cause of your issue but here are a few things that might help...

Before you do anything wrap $_GET['loc'] in the WordPress sanitize_text_field() function this will help prevent any script injection. See my code examples below.

I would then move the pre_get_posts action in your themes functions.php file.

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

And then in your archive template you had an unclosed tag (<p class="excerpt">). I've tided up your code, see below.

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

So, basically after WP 4.8.3 things got messed up.

Solution was 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');

Loop page:

$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 );

Basically course_dates is the repeater and the cd_location the field I need to get the information from.

Hope this helps someone.