Skip to content

Instantly share code, notes, and snippets.

@danielpataki
Last active December 1, 2015 19:42
Show Gist options
  • Save danielpataki/e12c354bc6c9c95c497d to your computer and use it in GitHub Desktop.
Save danielpataki/e12c354bc6c9c95c497d to your computer and use it in GitHub Desktop.
Post Filtering
global $wp_query;
$modifications = array();
if( !empty( $_GET['catname'] ) ) {
$modifications['category_name'] = $_GET['catname'];
}
$args = array_merge(
$wp_query->query_vars,
$modifications
);
query_posts( $args );
add_action( 'wp_enqueue_scripts', 'enqueue_parent_styles' );
function enqueue_parent_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' );
}
/*
Theme Name: Twenty Fifteen With Post Filter
Theme URI: http://yourwebsite.com/twentyfifteen-postfilter/
Description: My post filter project, based on Twenty Fifteen
Author: Daniel Pataki
Author URI: http://danielpataki.com
Template: twentyfifteen
Version: 1.0.0
Tags: black, green, white, light, dark, two-columns, three-columns, left-sidebar, right-sidebar, fixed-layout, responsive-layout, custom-background, custom-header, custom-menu, editor-style, featured-images, flexible-header, full-width-template, microformats, post-formats, rtl-language-support, sticky-post, theme-options, translation-ready, accessibility-ready, responsive-layout, infinite-scroll, post-slider, design, food, journal, magazine, news, photography, portfolio, clean, contemporary, dark, elegant, modern, professional, sophisticated
Text Domain: twenty-fourteen-child
*/
.post-filters {
margin: 0 8.3333% 8.3333% 8.3333%;
box-shadow: 0 0 1px rgba(0, 0, 0, 0.15);
padding:22px;
background: #fff;
}
<div class='post-filters'>
<select name="orderby">
<option value='post_date'>Order By Date</option>
<option value='post_title'>Order By Title</option>
<option value='rand'>Random Order</option>
</select>
<select name="order">
<option value='DESC'>Descending</option>
<option value='ASC'>Ascending</option>
</select>
<select name="thumbnail">
<option value='all'>All Posts</option>
<option value='only_thumbnailed'>Posts With Thumbnails</option>
</select>
</div>
<form class='post-filters'>
<select name="orderby">
<?php
$orderby_options = array(
'post_date' => 'Order By Date',
'post_title' => 'Order By Title',
'rand' => 'Random Order',
);
foreach( $orderby_options as $value => $label ) {
echo "<option ".selected( $_GET['orderby'], $value )." value='$value'>$label</option>";
}
?>
</select>
<select name="order">
<?php
$order_options = array(
'DESC' => 'Descending',
'ASC' => 'Ascending',
);
foreach( $order_options as $value => $label ) {
echo "<option ".selected( $_GET['order'], $value )." value='$value'>$label</option>";
}
?>
</select>
<select name="thumbnail">
<?php
$order_options = array(
'all' => 'All Posts',
'only_thumbnailed' => 'Posts With Thumbnails',
);
foreach( $order_options as $value => $label ) {
echo "<option ".selected( $_GET['thumbnail'], $value )." value='$value'>$label</option>";
}
?>
</select>
<input type='submit' value='Filter!'>
</form>
<option value='post_date' <?php if( ( !empty( $_GET['orderby'] ) && $_GET['orderby'] == 'post_date' ) || empty( $_GET['orderby'] ) ) echo "selected='selected'" ?>>Order By Date</option>
<option value='post_title' <?php if( ( !empty( $_GET['orderby'] ) && $_GET['orderby'] == 'post_title' ) ) echo "selected='selected'" ?>>Order By Title</option>
<option value='rand' <?php if( ( !empty( $_GET['orderby'] ) && $_GET['orderby'] == 'rand' ) ) echo "selected='selected'" ?>>Random Order</option>
<select name="orderby">
<?php
$orderby_options = array(
'post_date' => 'Order By Date',
'post_title' => 'Order By Title',
'rand' => 'Random Order',
);
foreach( $orderby_options as $value => $label ) {
echo "<option ".selected( $_GET['orderby'], $value )." value='$value'>$label</option>";
}
?>
</select>
global $wp_query;
$modifications = array();
if( !empty( $_GET['catname'] ) && $_GET['thumbail'] == 'only_thumbnailed' ) {
$modifications['meta_query'][] = array(
'key' => '_thumbnail_id',
'value' => '',
'compare' => '!='
);
}
$args = array_merge(
$wp_query->query_vars,
$modifications
);
query_posts( $args );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment