1

I'm still fairly new to the wonderful world of WordPress development and was hoping for help with a question..

I've got a simple page template which extracts all posts under a certain category and shows them in a defined order. All well and good, but what I'd like to do is add a filter so the user can select a child category from the list and then the page will refresh with just posts from the child category showing.

The Codex has a suggestion involving the wp_dropdown_category() function, but the action from that takes you to the category archive which I don't want; I need to keep the user within the page.

I use :

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array (
    'cat'            => 1,
    'posts_per_page' => 10,
    'paged'          => $paged
);
$query = new WP_Query( $args );

for the original selection and had in mind changing 'cat' => to the selected output from the list but can't get it right.

Any help gratefully appreciated!

  • but the action from that takes you to the category archive which I don't want. I don't understand what you mean by this. Why would category dropdown redirect to the selected archive page? – sakibmoon May 31 '15 at 11:46
  • Using the example from the codex, when you use the category drop down its action is to refresh to a category archive of the chosen category whereas I am using a page template and I want it to remain on that page but with a subsetted wp_query selection. – Philip May 31 '15 at 12:46
0

wp_dropdown_categories() function output a select box. You can wrap the output within a form with form action to the same page and upon user submitting the form, you can get the category id value and query for the posts within that category. Or you can use jQuery so that you don't need any form at all.

Codex has nice example for both of the case.

If you want to post the form to current page, you can use that page url as the form action like this

<li id="categories">
<h2><?php _e( 'Categories:' ); ?></h2>
<form id="category-select" class="category-select" action="<?php echo get_permalink($page_id); ?>" method="get">
<!-- $page_id is the current page id -->
    <?php wp_dropdown_categories( 'show_count=1&hierarchical=1&name=category_select' ); ?>
    <input type="submit" name="submit" value="view" />
</form>
</li>

You can use the returned value like this

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$cat = ( isset( $_GET['category_select'] ) ) ? $_GET['category_select'] : 1;
$args = array (
    'cat'            => $cat,
    'posts_per_page' => 10,
    'paged'          => $paged
);
$query = new WP_Query( $args );
  • Thanks. The action from the codex in those examples takes you away from the page to an archive page. I'm really looking for what action I need to remain on the page but with a subsetted query. – Philip May 31 '15 at 12:49
  • @Philip See my edit – sakibmoon May 31 '15 at 13:12
  • Thanks again. It reloads the page with "xxxx.co.uk/home/news/?category_select=8&submit=view" at the top, but ignores it and just loads the original categories. Any thoughts? – Philip May 31 '15 at 16:26
  • @Philip There was an error on my part. I've edited the answer. Check now. – sakibmoon May 31 '15 at 16:38
  • Still no joy. Thanks for trying. Just to confirm, I've got: 1. loop to get content from page, 2. your code, 3. new loop using $query->have_posts(). It still ignores the selection and sticks to the original selection. This is all from a page template. Do you think I'm missing something? – Philip May 31 '15 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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