Skip to content
This repository has been archived by the owner. It is now read-only.

Commit

Permalink
Post template filters
Browse files Browse the repository at this point in the history
Uses custom fields "categories", "authors", and "tags"
  • Loading branch information
szk11001 committed Jan 8, 2015
1 parent fc6065d commit 40c47d0
Showing 1 changed file with 45 additions and 5 deletions.
50 changes: 45 additions & 5 deletions page-blog.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,51 @@
<div class="col-sm-<?php echo (is_active_sidebar( $sidebar )?9:12); ?>">
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php the_content();?>
<?php query_posts('post_type=post&post_status=publish&paged='. get_query_var('paged')); ?>
<?php if( have_posts() ): ?>
<?php while( have_posts() ): the_post(); ?>

<?php
the_content();
$cat = get_post_meta( get_the_ID(), 'categories', true );
$tag = get_post_meta( get_the_ID(), 'tags', true );
$authornames = get_post_meta( get_the_ID(), 'authors', true );
$authorsArr = explode(",", $authornames);
if( array_key_exists(1, $authorsArr) ){
$authorsStr = '';
foreach( $authorsArr as $a ){
$user = get_user_by('slug', $a);
$id = $user->ID;
$authorsStr .= $id.',';
}
$author = rtrim($authorsStr, ',');
} else {
$author = get_user_by('slug', $authornames)->ID;
}
$args = array(
'post_type' => 'post',
'category_name' => $cat,
'tag' => $tag,
'author' => $author,
'post_status' => 'publish',
'paged' => get_query_var('paged'),
);
if( isset($args) ){
$pattern = '#[a-zA-Z|~`!@\#$%^&*()_+=|\\}\][{\'/;\-":?>.<]#';
$arr = array('category_name'=>$cat, 'tag'=>$tag, 'author'=>$author);
foreach( $arr as $k=>$v ){
if( empty($v) ){
continue;
}
if( $k == 'author' ){
$pattern = '#[0-9|~`!@\#$%^&*()_+=|\\}\][{\'/;\-":?>.<]#';
}
if( preg_match($pattern, $v) ){
$args[$k] = $v;
} else {
echo '<p>Invalid input in field: "'.$k.'"</p>';
}
}
}
query_posts( $args );
if( have_posts() ):
while( have_posts() ): the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
Expand Down

0 comments on commit 40c47d0

Please sign in to comment.