1

I'm building a Wordpress Theme and on the front page the user is able to select how they would like to order their posts. I've then created two functions that take the users selection through the variable $user_choice and develop a query based off of that choice.

However, I'm having trouble with scope and getting the following notices/errors:

Notice: Undefined variable: user_choice in /Users/Chappell/Websites/LFB/wp-content/themes/lfb/front-page-loop.php on line 3

Notice: Undefined variable: user_choice in /Users/Chappell/Websites/LFB/wp-content/themes/lfb/front-page-loop.php on line 5

Notice: Undefined variable: header_query in /Users/Chappell/Websites/LFB/wp-content/themes/lfb/front-page-loop.php on line 7

Fatal error: Call to a member function have_posts() on a non-object in /Users/Chappell/Websites/LFB/wp-content/themes/lfb/front-page-loop.php on line 7

front-page.php

    <?php get_header(); ?>
    
    <div class="container">
    
        <div class="row">
    
            <div class="col-md-8">
    
                <?php 
    
                    $user_choice = 'most-recent';
    
                    echo $user_choice;
                    
                    if (isset($_GET['formSubmit'])) {
    
                        $user_choice = $_GET['options'];
    
                    }
    
                    switch ($user_choice) {
                        case $user_choice === 'most-commented';
                            $title_text = 'most commented';
                        break;
                        case $user_choice === 'most-recent';
                            $title_text = 'most recent';
                        break;
                        case $user_choice === 'most-viewed';
                            $title_text = 'most viewed';
                        break;
                        case $user_choice === 'featured';
                            $title_text = 'featured';
                        break;
                        default: 
                            echo 'No posts were found matching this query.';
                    }   
    
                    ?>
    
                    <div class="row">
                        <div class="col-sm-8">
    
                        <h1 class="page-header home-header">Now displaying <?php echo $title_text . " Liverpool articles"; ?></h1>
    
                        </div><!--col-sm-8-->
                        <div class="col-sm-4">
                            <form role="form" class="form-inline pull-right orderer">
                                <div class="form-group">
                                    <label class="sr-only" for="options">Order By:</label>
                                    <select class="form-control order-select" name="options">
                                        <option value="most-recent" <?php if ($user_choice === 'most-recent') { echo 'selected'; }?>>Most Recent</option>
                                        <option value="most-commented" <?php if ($user_choice === 'most-commented') { echo 'selected'; }?>>Most Commented</option>
                                        <option value="most-viewed" <?php if ($user_choice === 'most-viewed') { echo 'selected'; }?>>Most Viewed</option>
                                        <option value="featured" <?php if ($user_choice === 'featured') { echo 'selected'; }?>>Featured</option>
                                    </select>
                                </div><!--form-group-->
                                <div class="form-group">
                                    <input type="submit" class="btn btn-default" id="orderer" value="Submit" name="formSubmit">
                                </div><!--form-group-->
                            </form>
                        </div><!--col-sm-4-->
                    </div><!--row-->
    
                    <?php get_template_part('front-page-loop'); ?>
                    
            </div><!--col-md-8-->
    
                <?php get_sidebar(); ?>
    
        </div><!--row-->
    
    </div><!--container -->
    
    <?php get_footer(); ?>

Front-page-loop.php

<?php 

/*  Takes the user choice from the home page select button and produces
*   a WP_Query relevant to that choice. Displays 1 posts per query.
*   @param string $user_choice
*/

function main_post_arg_finder($user_choice) {
    
    switch ($user_choice) {
        case $user_choice === 'most-commented';
            $args = array(
                'orderby' => 'comment_count',
                'posts_per_page' => 1
                );
        break;
        case $user_choice === 'most-recent';
            $args = array(
                'orderby' => 'date',
                'posts_per_page' => 1,
                'cat' => 239
                );
        break;
        case $user_choice === 'most-viewed';
            $args = array(
                'meta_key' => 'lfb_post_views_count',
                'orderby' => 'meta_value_num',
                'posts_per_page' => 1
                );
        break;
        case $user_choice === 'featured';
            $args = array(
                'orderby' => 'date',
                'posts_per_page' => 1,
                'category_name' => 'featured'
                );
        break;
        default: 
            echo 'No posts were found matching this query.';
    }   

    $header_query = new WP_Query($args);

    return $header_query;
}

/*  Takes the user choice from the home page select button and produces
*   a WP_Query relevant to that choice. Displays an unlimited amount of posts per
*   query.
*   @param string $user_choice
*/

function posts_arg_finder($user_choice) {
    switch ($user_choice) {
        case $user_choice === 'most-commented';
            $args = array(
                'orderby' => 'comment_count',
                );
        break;
        case $user_choice === 'most-recent';
            $args = array(
                'orderby' => 'date',
                'cat' => 239
                );
        break;
        case $user_choice === 'most-viewed';
            $args = array(
                'meta_key' => 'lfb_post_views_count',
                'orderby' => 'meta_value_num',
                );
        break;
        case $user_choice === 'featured';
            $args = array(
                'orderby' => 'date',
                'category_name' => 'featured'
                );
        break;
        default: 
            echo 'No posts were found matching this query.';
    }   

    $query = new WP_Query($args);

    return $query;

} 

?>

<div class="row">

    <?php echo $user_choice;
        
    main_post_arg_finder($user_choice);

        if ( $header_query->have_posts() ) : while ( $header_query->have_posts() ) : $header_query->the_post() ?>

                <?php $do_not_duplicate = $post->ID; ?>

                <div class="col-md-12">
                    
                        <a href="<?php the_permalink();?>"><h2><?php the_title();?></h2></a>
                        <?php the_post_thumbnail('thumbnail', array('class' => 'img-responsive')); ?>
                        <?php post_meta(); ?>
                        <?php the_excerpt(); ?>

                        <hr>

                </div><!--col-md-12-->

            <?php endwhile;

        else :

            echo '<p>No main post found</p>';

        endif; ?>

    </div><!--row-->

    <div class="row"> 

        <?php posts_arg_finder();

        $i = 0; 

         if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();

                if ( $post->ID == $do_not_duplicate ) continue;
            
                if ($i != 0 && $i % 1 == 0) { // add new row after 1 post
                    echo '</div><!--row--><div class="row">';
                }

                $i++; ?>

            <div class="col-md-12">

                    <a href="<?php the_permalink();?>"><h2><?php the_title();?></h2></a>
                    <?php post_meta(); ?>
                    <?php the_excerpt(); ?>

                    <hr>

            </div><!--col-md-12--> 

        <?php endwhile;

        else :

            echo '<p>No posts found</p>';

        endif; ?>

    </div><!--row-->

1 Answer 1

1

I have found the answer in another thread. Passing variables through locate_template

Essentially the WordPress function get_template_part changes the scope of the variable $user_choice. PHP default behaviour when using include() is that the variable from the current script is still available in the included file. However because get_template_part() is a function any variables in that template file now have local scope. Hence the errors.

1
  • Glad you worked it out - you can accept your own answer to mark this question as complete! Jul 27, 2016 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.