• I’m trying to filter my query by using a custom field I add to a page. I’m filtering by year. By adding a custom field “year” and the value. The problem seems to be that it’s not outputting the actual value into my query. I know this is the case because if I change ‘value’ => $year to ‘value’ => 2012 it changes my results. What am I doing wrong?

    My code is

    <?php $year = get_post_meta($post->ID, 'year', true); ?>
                		<?php $args = array( 'post_type' => 'cattle_champion',
                                'posts_per_page' => -1,
                                'order' => 'DESC',
                                'meta_query' => array(
                                    array(
                                        'key' => 'show_year',
                                        'value' => $year,
                                        'compare' => 'LIKE',
                                    ),
                                    array(
                                        'key' => 'show_level',
                                        'value' => 'county',
                                        'compare' => 'LIKE',
                                    )
                                )
                               );
    							$loop = new WP_Query( $args );
    							while ( $loop->have_posts() ) : $loop->the_post() ?>
Viewing 1 replies (of 1 total)
  • Try printing out the value of $year to see if it contains what you think it should. Add a line after the get_post_meta() line like this:

    <?php $year = get_post_meta($post->ID, 'year', true); ?>
    <?php print_r("<pre>YEAR =>$year<=</pre>"); ?>

    See what prints between ‘=>’ and ‘<=’. There should be nothing other than the 4 digits of the year.

Viewing 1 replies (of 1 total)
  • The topic ‘Do Meta Query based on Custom Field’ is closed to new replies.