• Resolved jonathan1138

    (@jonathan1138)


    Hi,

    I am using a combination of WP_Query with ACF to display a subset of data based on a general category (WP_Query loops through the whole category) and a sub-category (based on a field added using ACF for each of the items pulled in by the WP_Query).

    Ultimately I’d like one of the category buttons to assign the category to a variable which will be used in my WP_Query and if statement to display matching items only, on the same page. The page starts off displaying all items in all categories (the WP_Query and if statement take their initial arguments from parameters passed to the function called in the page code, I need to overwrite one of those parameter values).

    Is it possible, or more rather, can you help out a relative green-horn to have a button refresh the page with a sub-category assigned to a variable so that my existing query and if statement will display the subset of data requested? I’m just not sure how well WordPress is going to take to this very dynamic use of a single page, or if it would just be better to split out the sub-categories to individual WordPress pages (which I know I can do and will work, but it’s not very elegant or efficient).

    Here’s my button set (pulled from an ACF Select field):

    <div class="category-buttons">
                
                    <?php 
    
                        $choices = get_field_choices('website_category');
                        //var_dump($choices);
                        ?>
    
                        <ul class="website-categories">
                            <?php foreach ( $choices as $value => $label ): ?>
                                <li><a href=""><?php echo $label; ?></a></li>
                            <?php endforeach; ?>
                        </ul>
                        <?php //endif; ?>
    
            </div>

    (get_field_choices() is a custom function a clever person over on stackexchange coded to get the choices from the ACF post metadata).

    Here’s my main query loop:

     <?php if ($sub_category) { ?>
        
        <div class="portfolio">
            <?php 
    
                $args = array( 
                    'post_type' => 'portfolio',
                    'category_name' => $category,
                    'posts_per_page' => -1,
                    'orderby' => 'date',
                    'order' => 'ASC',
                );
    
                $portfolio_query = new WP_Query( $args );
    
                $i = 1;
    
                while($portfolio_query->have_posts()): $portfolio_query->the_post(); 
                
                if ((get_field('website_category') == $sub_category) && ($i <= $limit)) {
                $i++;
              // html to display the results here 

    I’m thinking there might be some built-in WordPress function I don’t know about, or some simple javascript, just need pointing in the right direction.

    • This topic was modified 3 years, 4 months ago by jonathan1138.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi there

    I think you just need to construct a link to the current page itself with an additional parameter attached to it (add_query_arg() will be helpful), based off which you decide what changes you need to make in your code to fetch corresponding data.

    WordPress will play nice to it, including any caching plugin as URLs will be treated as different pages.

    Thread Starter jonathan1138

    (@jonathan1138)

    Thank you! add_query_arg() certainly put me on the right track.

    FWIW I had to make use of the additional function shown on the get_query_var() for custom query vars, shown on this page (added to my functions.php).

    My button link looks like this:
    <a href="<?php echo esc_url( add_query_arg('category',$value) ); ?>"><?php echo $label; ?></a>

    So I can then get the custom query var like this:
    $sub_category = get_query_var( 'category' );

    You would be better off with a specific custom name to your query var than just category because of possible conflicts.

    Also be mindful of whether you are editing the main query or creating a secondary query to render content on the page. In your post above, where you say main query loop, you are actually not using the main query loop which happens behind the scenes but creating a new instance of WP_Query.

    Typically you want to use custom vars when wanting custom parameters to modify an existing query of the page, which seems to be your use case. You can certainly use it for modifying or feeding second query on the page. It can be a little tricky to wrap your head around it. It certainly was for me when I discovered them. This was an excellent refresher for me – https://wpmudev.com/blog/building-customized-urls-wordpress/

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Button to refresh the page with a value assigned to a variable?’ is closed to new replies.