Wordpress custom taxonomy pagination not working - wordpress

I’m using the WP PageNavi plugin for pagination. This particular problem in not getting the taxonomy-portflio-category.php page to paginate is also a problem when WP PageNavi is turned off.
I’ve had a heck of a time getting pagination to work on the homepage and on a page template page, but I did get them to work. Here’s their code:
page-home.php (used as a Page template on a static front page called “Home”)
$paged = 1;
if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');
$i = 0;
$loop = new WP_Query( array( 'post_type' => 'portfolio', 'paged' => $paged, 'posts_per_page' => 24 ) );
while ( $loop->have_posts() ) : $loop->the_post();
// output
$i++; endwhile;
if ( function_exists( 'wp_pagenavi' ) ) {
wp_pagenavi( array( 'query' => $loop ) );
wp_reset_postdata();
}
Pagination works!
page-portfolio.php (used as a Page template on a Page called “Work”)
$i = 0;
$loop = new WP_Query( array( 'post_type' => 'portfolio', 'paged' => get_query_var( 'paged' ), 'posts_per_page' => 24 ) );
while ( $loop->have_posts() ) : $loop->the_post();
// output
$i++; endwhile;
if ( function_exists( 'wp_pagenavi' ) ) {
wp_pagenavi( array( 'query' => $loop ) );
wp_reset_postdata();
}
Pagination works!
taxonomy-portfolio-category.php (used as a way to display portfolio sections e.g. print, photography, etc.)
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
global $wp_query;
query_posts( array_merge( $wp_query->query, array( 'posts_per_page' => 2 ) ) );
if (have_posts()) : while ( have_posts() ) : the_post();
// output
endwhile; endif;
if ( function_exists( 'wp_pagenavi' ) ) {
wp_pagenavi();
}
Page 1 (/portfolio/interactive/) looks great! It’s definitely only posting 2 items and it calculates the correct number of pagination pages. But when you click on page 2 or 3 or 4 (/portfolio/interactive/page/2/) the site defaults to index.php and shows “Page not found”. Pagination fails!
Hopefully I can resolve this soon. I’ve seen A LOT of people with this same problem of pagination on custom taxonomy pages, but no solid solutions. Please help!

You Need to set posts per page to 24 on the Settings -> Reading page in WP admin. Hope this helps someone.

I have tried using WP-Pagenavi but it never worked so i used the pagination from Wordpress it self, i used the twentyfourteen_paging_nav() function form Twentyfourteen becuse it has a taxonomy page, here is the code:
if ( ! function_exists( 'twentyfourteen_paging_nav' ) ) :
function twentyfourteen_paging_nav() {
global $wp_query, $wp_rewrite;
// Don't print empty markup if there's only one page.
if ( $wp_query->max_num_pages < 2 ) {
return;
}
$paged = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1;
$pagenum_link = html_entity_decode( get_pagenum_link() );
$query_args = array();
$url_parts = explode( '?', $pagenum_link );
if ( isset( $url_parts[1] ) ) {
wp_parse_str( $url_parts[1], $query_args );
}
$pagenum_link = remove_query_arg( array_keys( $query_args ), $pagenum_link );
$pagenum_link = trailingslashit( $pagenum_link ) . '%_%';
$format = $wp_rewrite->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
$format .= $wp_rewrite->using_permalinks() ? user_trailingslashit( $wp_rewrite->pagination_base . '/%#%', 'paged' ) : '?paged=%#%';
// Set up paginated links.
$links = paginate_links( array(
'base' => $pagenum_link,
'format' => $format,
'total' => $wp_query->max_num_pages,
'current' => $paged,
'mid_size' => 1,
'add_args' => array_map( 'urlencode', $query_args ),
'prev_text' => __( '← Previous', 'twentyfourteen' ),
'next_text' => __( 'Next →', 'twentyfourteen' ),
) );
if ( $links ) :
?>
<nav class="pagination-contaner" role="navigation">
<h1 class="screen-reader-text"><?php _e( 'Posts navigation', 'twentyfourteen' ); ?></h1>
<ul class="pagination">
<?php echo $links; ?>
</ul><!-- .pagination -->
</nav><!-- .navigation -->
<?php
endif;
}
endif;

I ran into similar issue, it took me hours of googling! I found the solution at last.
Add the following code to functions.php in your theme folder:
$option_posts_per_page = get_option( 'posts_per_page' );
add_action( 'init', 'my_modify_posts_per_page', 0);
function my_modify_posts_per_page() {
add_filter( 'option_posts_per_page', 'my_option_posts_per_page' );
}
function my_option_posts_per_page( $value ) {
global $option_posts_per_page;
if ( is_tax( 'portfolio-category') ) {
return 2;
} else {
return $option_posts_per_page;
}
}
URL for the solution

May be you need to enable search to enable pagination
While declaring custom taxonomy you should disable search excluding.
exclude_from_search => false
This fixed my problem.

I would like to share following solution (add this code to functions.php in your theme):
function fix_taxonomy_pagination ( $query ) {
// not an admin page and it is the main query
if (!is_admin() && $query->is_main_query()){
if(is_tax()){
// where 24 is number of posts per page on custom taxonomy pages
$query->set('posts_per_page', 24);
}
}
}
add_action( 'pre_get_posts', 'fix_taxonomy_pagination' );
source

Related

pagination in page with custom code in wordpress

How to add pagination for comments in page with custom code in Wordpress?
My code:
<?php
<ol class="comment-list">
<?php
$comments = get_comments(array(
'number' => '8',
'status' => 'approve',
'offset' => $offset
));
foreach( $comments as $comment ){
echo $comment->comment_author . '<br />' . $comment->comment_content;
}
?>
</ol>
<?php paginate_comments_links ( array( 'prev_text' => '« PREV' , 'next_text' => 'NEXT »' ) ); ?>
I searched here for solutions, but did not find any. There are old posts from before 2014. Other codes given here do not work. I have been struggling for several hours.
Please help.
How are you setting the $offset variable? Try something like this, code is not tested but it will give you idea to get comments pagination working.
<?php
$current_page = get_query_var( 'cpage' ) ? get_query_var( 'cpage' ) : 1;
$comments_per_page = 8;
$offset = ( $current_page - 1 ) * $comments_per_page;

Display Users attached to Custom Post Type - ACF

I am trying to display users attached to a Custom Post Type. I created an ACF-Users-Field which returns the ID. This field is displayed at my Custom Post Type. Now I would like to display the names of the users selected in this field.
I am able to display all names of my users (if I remove the part 'meta_query' of my arguments, but if i add the meta_query array to my arguments nothing is displayed. I am not sure, maybe the mistake is the 'value', but I have no idea what to change there. Below I show the relevant code:
<article>
<?php
$args = array(
'role' => 'Subscriber',
);
$my_user_query = new WP_User_Query( $args );
$editors = $my_user_query->get_results();
if ( ! empty( $editors ) ) {
foreach ( $editors as $editor ) {
$editor_info = get_userdata( $editor->ID );
$args = array(
'post_type' => 'projekt',
'meta_query' => array(
array(
'key' => 'projektteilnehmer', // name of custom field - return ID
'value' => $post->ID ,
'compare' => 'LIKE'
)
)
);
// The User Query
$user_query = new WP_User_Query( $args );
// The User Loop
if ( ! empty( $user_query->results ) ) { ?>
<?php foreach ( $user_query->results as $user ) {
echo $user->user_firstname;;
}
} else {
echo 'nothing found';
}
} // endforeach
} else {
echo __( 'Kein Mitglied gefunden.' );
}
?>
</article>
Any help appreciated
By what your describing this code should do what you want. given that the participant's are returned as userID
<article>
<?php
$teilnehmers = get_field('projektteilnehmer', $post->ID);
if ( ! empty( $teilnehmers ) ) {
foreach ( $teilnehmers as $teilnehmer ) {
$user = get_user_by('ID', $teilnehmer);
if($user){
echo $user->user_firstname;;
}
}
} else {
echo __( 'Kein Mitglied gefunden.' );
}
?>
</article>
or if they are returned as user objects
<article>
<?php
$teilnehmers = get_field('projektteilnehmer', $post->ID);
if ( ! empty( $teilnehmers ) ) {
foreach ( $teilnehmers as $teilnehmer ) {
echo $teilnehmer->user_firstname;;
}
} else {
echo __( 'Kein Mitglied gefunden.' );
}
?>
</article>
The answer of Jasper B pushed me in the right direction. Thank you very much:) It is working with this query:
<?php // projekte
$projekte = get_posts(array(
'post_type' => 'projekt',
'meta_query' => array(
array(
'key' => 'projektteilnehmer', // name of custom field - return value id
'value' => $editor_info->ID,
'compare' => 'LIKE'
)
)
));
?>
<?php if( $projekte ): ?>
<strong>Projekte:</strong>
<ul>
<?php foreach( $projekte as $projekt ):?>
<li>
<a href="<?php echo get_permalink( $projekt->ID ); ?>">
<?php echo get_the_title( $projekt->ID ); ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif;
// ende projekte ?>

First and Last class within product loop issue

Im running a simple custom product loop. The issue im having is that the LAST class is set at the wrong time. It seems the loop_index is not correct. I should have 4 products per row, but after the 3rd the last class is set. Any ideas what the issue could be?
Thanks
$args = array(
'post_type' => 'product',
'posts_per_page' => 40
);
$loop = new WP_Query($args);
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
Please add a filter (loop_shop_columns) to overwrite shop columns.
function loop_columns() {
return 4;
}
add_filter('loop_shop_columns', 'loop_columns', 999); //overwrite shop column filter
$args = array(
'post_type' => 'product',
'posts_per_page' => 40
);
$loop = new WP_Query($args);
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
make sure to call add_filter in relevant function only. Otherwise it will affect to all archive/category pages.
woocommerce_reset_loop();
Add this before args

Products dropdown from same category inside Woocommerce product short description

In Woocommerce, I would like to add a drop down in product short description that shows all products that have the same category(ies). It will be even better if it was possible to go to the product page of the selected product.
I didn't see any threads that fulfill what I am trying to do.
Any help will be appreciated.
2021 Update - Added product_id as argument, allowing the shortcode to be used for a defined product ID (for example on a page).
The following will make a custom shortcode that you can use in your product short description (or even in the product description) and will display a dropdown from same product category than the current product.
The code:
add_shortcode( 'products_dropdown', 'wc_products_from_cat_dropdown' );
function wc_products_from_cat_dropdown( $atts ) {
// Shortcode Attributes
$atts = shortcode_atts( array(
'product_id' => '',
), $atts, 'products_dropdown' );
$product_id = is_product() ? get_the_id() : $atts['product_id'];
if ( empty($product_id) )
return;
ob_start();
$query = new WP_Query( array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => '-1',
'post__not_in' => array( $product_id ),
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'ids' ) ) ,
) ),
) );
if ( $query->have_posts() ) :
echo '<div class="products-dropdown"><select name="products-select" id="products-select">
<option value="">'.__('Choose a related product').'</option>';
while ( $query->have_posts() ) : $query->the_post();
echo '<option value="'.get_permalink().'">'.get_the_title().'</option>';
endwhile;
echo '</select> <button type="button" style="padding:2px 10px; margin-left:10px;">'._("Go").'</button></div>';
wp_reset_postdata();
endif;
?>
<script type='text/javascript'>
jQuery(function($){
var a = '.products-dropdown', b = a+' button', c = a+' select', s = '';
$(c).change(function(){
s = $(this).val();
console.log(s); // just for testing (to be removed)
});
$(b).click(function(){
if( s != '' ) location.href = s;
});
});
</script>
<?php
return ob_get_clean();
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
USAGE
1). For single product pages: Just paste the following shortcode in the product short description (or descrition):
[products_dropdown]
2). For single product pages, inside php code:
echo do_shortcode("[products_dropdown]");
3). on any post or page within the text editor, define the product_id argument (below the defined product id is 37):
[products_dropdown product_id="37"]
Add this to your theme's 'functions.php' which will display all products of your current product's category.
function add_products_short_description() {
$product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
if ( $product_cats ) {
$single_cat = array_shift( $product_cats );
$product_args = array( 'post_type' => 'product', 'posts_per_page' => '-1', 'product_cat' => $single_cat->name );
$products = new WP_Query( $product_args );
if ( $products->have_posts() ) : echo '<ul>';
while ( $products->have_posts() ) : $products->the_post(); global $product;
echo '<li>'.get_the_title($products->ID).'</li>';
endwhile;
echo '</ul>';
endif;
}
}
add_action( 'woocommerce_single_product_summary', 'add_products_short_description', 15 );

wordpress post query loop config

I'm new to loop customisation and I have an issue getting my search page front end filter loop working in the same way as my other category template post loop.
This is the loop I am using for the category templates which works fine.
<?php $custom_query_args = array( 'cat' => '2', 'posts_per_page' => 6 );
$custom_query_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$custom_query = new WP_Query( $custom_query_args );
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $custom_query;
if ( $custom_query->have_posts() ) :
while ( $custom_query->have_posts() ) :
$custom_query->the_post(); ?>
This allows me to paginate through all posts in this category but I need to convert a similar code block on my search page to allow it to do the same, currently the search template code below doesn't paginate like the above.
<?php wp_reset_postdata();
global $wp_query;
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
foreach($_POST['filter_cat'] as $fcat){$fcat_list .= $fcat . ",";}
query_posts(
array_merge(
array(
'cat' => $fcat_list,
'posts_per_page' => 6,
'paged' => $paged
),
$wp_query->query
)
); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
I know that the first code block is a better method but I can't figure out how to use the second block in the same way.
Edit:
Ok please excuse my inexperience.
Thanks to Pieter Goosen I'm part way there with using pre_get_posts.
I've done so successfully with category template but using the following for the search page template kills the site, I'm not sure where I'm going wrong.
function paginate_search ( $query ) {
if( $query->!is_admin() ) {
foreach($_POST['filter_cat'] as $fcat){$fcat_list .= $fcat . ",";}
query_posts( array_merge ( array (
'cat' => $fcat_list,
'posts_per_page' => 6,
'paged' => $paged
),
$wp_query->query
) ); } }
add_action( 'pre_get_posts', 'paginate_search' );

Resources