Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

TL:DR How do I specify a dynamic meta value for search queries?

I have [this archive page] that allows to do search within filtered taxonomies 1:

<?php
$term = $wp_query->get_queried_object();
?>

<script>console.log(<?php addslashes(htmlspecialchars(var_dump($wp_query)));?></script>

<?php $post_type = ( isset( $_GET['post_type'] ) ) ? $_GET['post_type'] : null;
$current_speaker = get_query_var('sermon_speaker');
$speaker_terms = get_terms('sermon_speaker');
$current_series = get_query_var('sermon_series');
$series_terms = get_terms('sermon_series');
$current_format = get_query_var('sermon_format');
$format_terms = get_terms('sermon_format');
$current_topic = get_query_var('sermon_topic');
$topic_terms = get_terms('sermon_topic');

$sermon_settings = get_option('ct_sermon_settings');
isset($sermon_settings['archive_filter_1']) ? $ct_sermon_filters_speaker = $sermon_settings['archive_filter_1'] : $ct_sermon_filters_speaker = null;
isset($sermon_settings['archive_filter_2']) ? $ct_sermon_filters_series = $sermon_settings['archive_filter_2'] : $ct_sermon_filters_series = null;
isset($sermon_settings['archive_filter_3']) ? $ct_sermon_filters_format = $sermon_settings['archive_filter_3'] : $ct_sermon_filters_format = null;
isset($sermon_settings['archive_filter_4']) ? $ct_sermon_filters_topic = $sermon_settings['archive_filter_4'] : $ct_sermon_filters_topic = null;
isset($sermon_settings['archive_filter_5']) ? $ct_sermon_filters_keyword = $sermon_settings['archive_filter_5'] : $ct_sermon_filters_keyword = null;
$ct_sermon_filters_button_text = $sermon_settings['archive_filters_button_text'];
if(empty($ct_sermon_filters_button_text)) $ct_sermon_filters_button_text = 'Search Sermons';
$ct_sermon_archive_title = $sermon_settings['archive_title'];
if(empty($ct_sermon_archive_title)) $ct_sermon_archive_title = 'Sermon Archives';
$ct_sermon_archive_slug = $sermon_settings['archive_slug'];
if(empty($ct_sermon_archive_slug)) $ct_sermon_archive_slug = 'sermons';
$ct_sermon_archive_tagline = $sermon_settings['archive_tagline'];
$ct_sermon_archive_layout = $sermon_settings['archive_layout'];
?>

<?php get_header(); ?>

<div class="content-title">

    <?php $post = $posts[0]; // Hack. Set $post so that the_date() works. ?>
        <?php if(is_post_type_archive('ct_sermon') || $post_type == 'ct_sermon' || is_tax('sermon_speaker') || is_tax('sermon_series') || is_tax('sermon_format') || is_tax('sermon_topic')) { echo $ct_sermon_archive_title; ?>        
        <?php /* If this is a category archive */ } elseif (is_category()) { ?>
        <?php printf(__('%s'), single_cat_title('', false)); ?>
        <?php /* If this is a tag archive */ } elseif( is_tag() ) { ?>
        <?php printf(__('Posts tagged &quot;%s&quot;'), single_tag_title('', false) ); ?>
        <?php /* If this is a daily archive */ } elseif (is_day()) { ?>
        <?php printf(_c('Daily archive %s'), get_the_time(__('M j, Y'))); ?>
        <?php /* If this is a monthly archive */ } elseif (is_month()) { ?>
        <?php printf(_c('Monthly archive %s'), get_the_time(__('F, Y'))); ?>
        <?php /* If this is a yearly archive */ } elseif (is_year()) { ?>
        <?php printf(_c('Yearly archive %s'), get_the_time(__('Y'))); ?>
        <?php /* If this is an author archive */ } elseif (is_author()) { ?>
        Author Archive'); ?>
        <?php /* If this is a paged archive */ } elseif (isset($_GET['paged']) && !empty($_GET['paged'])) { ?>
        Blog Archives'); ?>
        <?php } ?>

        <span class="searched"><?php foreach ($speaker_terms as $term) { if($current_speaker == $term->slug) { echo $term->name; } } ?>
                        <?php if($current_speaker && $current_series) { echo ' + '; } ?>
                        <?php foreach ($series_terms as $term) { if($current_series == $term->slug) { echo $term->name; } } ?>
                        <?php if(($current_speaker || $current_series) && $current_format) { echo ' + '; } ?>
                        <?php foreach ($format_terms as $term) { if($current_format == $term->slug) { echo $term->name; } } ?>
                        <?php if(($current_speaker || $current_series || $current_format) && $current_topic) { echo ' + '; } ?>
                        <?php foreach ($topic_terms as $term) { if($current_topic == $term->slug) { echo $term->name; } } ?>
                        <?php if(($current_speaker || $current_series || $current_format || $current_topic) && $search_query) { echo ' + '; } ?>
                        <?php if($search_query && ($post_type == 'ct_sermon' || $current_speaker || $current_series || $current_format || $current_topic)): echo '&quot;'.$search_query.'&quot;'; endif; ?></span>

    <a href="javascript: void(0);" id="mode list"></a>
</div>

<?php if(is_post_type_archive('ct_sermon') || $post_type == 'ct_sermon' || is_tax('sermon_speaker') || is_tax('sermon_series') || is_tax('sermon_format') || is_tax('sermon_topic')):

?>
<?php if($ct_sermon_filters_speaker || $ct_sermon_filters_series || $ct_sermon_filters_format || $ct_sermon_filters_topic || $ct_sermon_filters_keyword): ?>
                <form method="get" id="sermon-filter" action="<?php echo home_url('/'); ?><?php echo $ct_sermon_archive_slug; ?>">
                    <div id="sermon_filter">
<?php if($ct_sermon_filters_speaker): ?>
                        <div>
                            <select name="sermon_speaker" id="sermon_speaker" style="display:none;">
                                <option value="">Any Speaker</option>
                                <?php dropdown_taxonomy_term('sermon_speaker'); ?>
                            </select>
                        </div>
<?php endif; ?>
<?php if($ct_sermon_filters_series): ?>
                        <div>
                            <select name="sermon_series" id="sermon_series" style="display:none;">
                                <option value="">Any Series/Venue</option>
                                <?php dropdown_taxonomy_term('sermon_series'); ?>
                            </select>
                        </div>
<?php endif; ?>
<?php if($ct_sermon_filters_format): ?>
                        <div>
                            <select name="sermon_format" id="sermon_format" style="display:none;">
                                <option value="">Any Format</option>
                                <?php dropdown_taxonomy_term('sermon_format'); ?>
                            </select>
                        </div>
<?php endif; ?>
<?php if($ct_sermon_filters_topic): ?>
                        <div>
                            <select name="sermon_topic" id="sermon_topic" style="display:none;">
                                <option value="">Any Topic</option>
                                <?php dropdown_taxonomy_term('sermon_topic'); ?>
                            </select>
                        </div>
<?php endif; ?>
                        <div>
                            <select name="sermon_passage" id="sermon_passage" style="display:none;">
                                <option value="">Any Passage</option>
                                <?php dropdown_taxonomy_term('sermon_topic'); ?>
                            </select>
                        </div>
<?php if($ct_sermon_filters_keyword): ?>
                        <div>
                            <input type="hidden" name="post_type" value="ct_sermon" />
                            <input type="text" name="s" size="20" placeholder="Search terms" value="<?php echo $s; ?>" class="sermon_keywords" />
                        </div>
<?php endif; ?>
                        <div><input type="submit" name="submit" class="button" value="<?php echo $ct_sermon_filters_button_text; ?>" /></div>
                    </div>
                </form>
<?php 
    get_template_part('loop','ct_sermon');
    endif; 
    else: get_template_part('loop');
?>
<?php
endif; ?>                       


<?php get_template_part('pagination'); ?>

<?php get_footer(); ?>

function that does the magic is:

// Custom taxonomy terms dropdown function
if(!function_exists('dropdown_taxonomy_term')) {
    function dropdown_taxonomy_term($taxonomy) {
        $terms = get_terms($taxonomy);
        foreach ($terms as $term) {
            $term_slug = $term->slug;
            $current_speaker = get_query_var('sermon_speaker');
            $current_series = get_query_var('sermon_series');
            $current_format = get_query_var('sermon_format');
            $current_topic = get_query_var('sermon_topic');
            if($term_slug == $current_speaker || $term_slug == $current_series || $term_slug == $current_format || $term_slug == $current_topic) {
                echo '<option value="'.$term->slug.'" selected>'.$term->name.'</option>';
            } else {
                echo '<option value="'.$term->slug.'">'.$term->name.'</option>';
            }
        }
    }
}

I would like to be able to add another drop down that allows the user to filter post by the presence a meta value in one of several fields. These values are set by a static dropdown in the first place, so it would be fine for the filter to be hardcoded. But it does need to work in tandem with the taxonomy filters.

The would the books in the Bible, e.g.:

<option value="" selected>- Select Book -</option>
            <option value="Genesis>Genesis</option>
            <option value="Exodus">Exodus</option>
            <option value="Leviticus">Leviticus</option>
            <option value="Numbers">Numbers</option>
            <option value="Deuteronomy">Deuteronomy</option>
            <option value="Joshua">Joshua</option>
            ...

I know how meta_query works, I just don't understand how to hook it up into a search page like this.

Thoughts?

share|improve this question
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.