I am trying to list woocommerce product by author. I followed this steps:
entered this code on theme/functions.php
add_action('init', 'wpse_74054_add_author_woocommerce', 999 );
function wpse_74054_add_author_woocommerce() {
add_post_type_support( 'product', 'author' );
}
This let me assigned an author to each product on the edit product tab.
I am able also able to display the product title the author and description using this code:
<?php $loop = new WP_Query( array ( 'post_type' => 'product', 'posts_per_page' => 10 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php the_title( '<h2 class="entry-title"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a></h2>' ); ?>
<span class="postauthortop author vcard">
<i class="icon-user"></i> <?php echo __('by', 'virtue');?> <a href="<?php echo get_author_posts_url(get_the_author_meta('ID')); ?>" rel="author"><?php echo get_the_author() ?></a>
<div class="short_description">
<?php echo apply_filters( 'woocommerce_short_description', $post->post_excerpt ) ?>
</div>
<?php endwhile; ?>
However, when people click on author's name only regular post will be shown. Not products.
I want to creat an author page where people can see a list of product by a singular author.
I have tried creating an author.php file using the following code, but is not working.
<?php $author_id = the_author_meta('ID'); ?>
<?php $args = array( $author_id => 'author' , 'post_type' => 'product' ); ?>
<?php $author_posts = get_posts ( $args ); ?>
<?php while( $author_posts->have_posts() ) : $author_posts->the_post(); ?>
<?php the_title( '<h2 class="entry-title"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a></h2>' ); ?>
<?php wp_reset_postdata(); ?>
<?php endwhile; ?
If anyone knows to write this code, please help me. Please, be specific as I am not familiar with coding.
Thanks in advance.
Why don't you try to specify the post_type
to be product
globally when on is_author()
?
It would be something like this (add this code to your functions.php):
<?php
add_action('pre_get_posts', 'my_pre_get_posts');
function my_pre_get_posts($query) {
if ($query->is_author() && $query->is_main_query()) {
$query->set('post_type', 'product');
}
}
?>
FYI, from the code blocks that you've submitted in your question, leave the first one (as you will still need to set the author for particular products), but you probably won't be needing the second and the third code block.
EDIT
To display products per author in a separate URL, insert the following code into your functions.php:
add_action('generate_rewrite_rules', 'my_custom_rewrite_rules');
function my_custom_rewrite_rules($rewrite) {
$rewrite->rules = array(
'author-products/([^/]+)/?$' => 'index.php?author_name=$matches[1]&post_type=product'
) + $rewrite->rules;
}
then go to Settings -> Permalinks
and click the Save Changes
button.
Your URLs will then be like: http://example.com/author-products/admin/