I'd like to include in my website a feature for users to only see posts from a certain author and a certain category. I'm able to do it separately but I can't seem to understand how to do it simultaneously.
If you want your visitors to choose the category and author you can use the code below.
If your loop in the template hasn't been changed you should get away with adding a query to the URL like so:
http://website.com/post-title/?author=1&cat=1
If you have a custom query you could do the following:
$author = $_GET['author']; //Get Author ID from query
$cat = $_GET['cat']; //Get Category ID from query string
$args = array(
'posts_per_page' => 10
);
if ( $author ) { //If $author found add it to custom query
$args['author'] = $author;
}
if ( $cat ) { //If $cat found add it to custom query
$args['cat'] = $cat;
}
$custom_query = new WP_Query( $args );
if ( $custom_query->have_posts() ) :
while ( $custom_query->have_posts() ) : $custom_query->the_post();
//post stuff
endwhile;
else :
echo 'No posts found...';
endif;
Then in your template:
<form class="post-filters">
<select name="author">
<?php
$author_options = array(
'1' => 'Author 1',
'2' => 'Author 2',
'3' => 'Author 3'
);
foreach( $orderby_options as $value => $label ) {
echo '<option ' . selected( $_GET['author'], $value ) . ' value="' . $value . '">$label</option>';
}
?>
</select>
<select name="cat">
<?php
$cat_options = array(
'1' => 'Category 1',
'2' => 'Category 2'
);
foreach( $orderby_options as $value => $label ) {
echo '<option ' . selected( $_GET['cat'], $value ) . ' value="' . $value. '">$label</option>';
}
?>
</select>
<button type="submit">Show Posts</button>
</form>
Else (forced to specific author / category basically what talentedaamer said)
$args = array(
'posts_per_page' => 10,
'author' => 1, //Author ID
'cat' => 1 //Category ID
);
$custom_query = new WP_Query( $args );
if ( $custom_query->have_posts() ) :
while ( $custom_query->have_posts() ) : $custom_query->the_post();
//post stuff
endwhile;
else :
echo 'No posts found...';
endif;
use WP_Query() author parameters & category parameters
similarly for certain category use WP_Query() category parameters.
if category is custom taxonomy then use the tax_query patameter in WP_Query().
<?php
/**
* Query Posts with WP_Query.
* For complete list of parametes follow link below.
* CODEX: http://codex.wordpress.org/Class_Reference/WP_Query
*/
$args = array(
/**
* Author id : (int) author id.
* use - to excluse author ID
* e.g -1 will exclude -1 authors posts.
*/
'author' => 1,
/**
* Author name : use 'user_nicename' (NOT name)
*/
'author_name' => 'admin',
/**
* Show posts associated with certain category.
* cat : (int) - use category id.
*/
'cat' => 5,
/**
* category_name : (string) - use category slug (NOT name).
* can also pass comma seperated list
* e.g 'news', 'events', etc...
*/
'category_name' => 'news', //
'posts_per_page' => 10,
'order' => 'DESC',
'orderby' => 'date',
);
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
// do something...
endwhile;
endif;
// Reset Post Data
wp_reset_postdata();
?>
-
1
-