Wordpress - Custom Taxonomy Pagination - wordpress

I'm using:
Wordpress 3.4
WP-PageNavi 2.82
I register custom taxonomy (catalog)
<?php
add_action('init', 'pca_register_taxonomy', 0);
function pca_register_taxonomy()
{
register_taxonomy('catalog', null,
array(
'label' => __('Catalogs', __),
'labels' => array(
'name' => __('Catalogs', __),
'singular_name' => __('Catalog', __),
'search_items' => __('Search Catalogs', __),
'popular_items' => __('Popular Catalogs', __),
'all_items' => __('All Catalogs', __),
'parent_item' => __('Parent Catalog', __),
'parent_item_colon' => __('Parent Catalog', __),
'edit_item' => __('Edit Catalog', __),
'update_item' => __('Update Catalog', __),
'add_new_item' => __('Add New Catalog', __),
'new_item_name' => __('New Catalog Name', __),
'separate_items_with_commas' => __('Separate catalogs with commas', __),
'add_or_remove_items' => __('Add or remove catalogs', __),
'choose_from_most_used' => __('Choose from the most used catalogs', __),
'menu_name' => __('Catalogs', __)
),
'public' => true,
'show_in_nav_menus' => true,
'show_ui' => true,
'show_tagcloud' => false,
'hierarchical' => true,
'query_var' => true,
'rewrite' => array(
'slug' => 'catalog',
'with_front' => true,
'hierarchical' => true
),
'capabilities' => array(
'manage_terms',
'edit_terms',
'delete_terms',
'assign_terms'
)
)
);
}
?>
I register custom post type (product)
<?php
add_action('init', 'pca_register_post_type');
function pca_register_post_type()
{
register_post_type('product',
array(
'label' => __('Products', __),
'labels' => array(
'name' => __('Products', __),
'singular_name' => __('Product', __),
'add_new' => __('Add New', __),
'add_new_item' => __('Add New Product', __),
'edit_item' => __('Edit Product', __),
'new_item' => __('New Product', __),
'all_items' => __('All Products', __),
'view_item' => __('View Product', __),
'search_items' => __('Search Products', __),
'not_found' => __('No products found', __),
'not_found_in_trash' => __('No products found in Trash', __),
'parent_item_colon' => '',
'menu_name' => __('Products', __)
),
'description' => '',
'public' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_nav_menus' => true,
'show_in_menu' => true,
'show_in_admin_bar' => true,
'menu_position' => 20,
'capability_type' => 'post',
'meta_cap' => true,
'hierarchical' => false,
'supports' => array(
'title',
'editor',
'thumbnail',
'page-attributes',
'post-formats'
),
'taxonomies' => array('catalog'),
'has_archive' => false,
'rewrite' => array(
'slug' => 'products',
'with_front' => true,
'feeds' => false,
'pages' => true
),
'query_var' => true,
'can_export' => true
)
);
}
?>
Then I create a new file for tax -> taxonomy-catalog.php
In this file, I query all products (custom post type) from specified catalog (tax):
<?php
$paged = get_query_var('paged');
$paged = ($paged) ? $paged : 1;
$products = new WP_Query(array(
'catalog' => $catalog_data->slug, // $catalog_data is the current taxonomy (woman)
'post_type' => 'product',
'posts_per_page' => 12,
'paged' => $paged
));
?>
<?php while ($products->have_posts()) : $products->the_post(); ?>
// Show title, content ... everything ok
<?php endwhile; ?>
<?php if (function_exists('wp_pagenavi')) wp_pagenavi(array('query' => $products)); ?>
<?php wp_reset_postdata(); ?>
Pagination is displayed correctly but when I click on page 2 or over I have a 404 error.
Works -> mywebsite.com/catalog/woman
Works not -> mywebsite.com/catalog/woman/page/2
I already refreshed permalinks.
Any idea to fix this ? thanks

Put this into your "functions.php" and then regenerate permalinks.
It works to me!
function taxonomy_rewrite_fix($wp_rewrite) {
$r = array();
foreach($wp_rewrite->rules as $k=>$v){
$r[$k] = str_replace('catalog=$matches[1]&paged=','catalog=$matches[1]&page=',$v);
}
$wp_rewrite->rules = $r;
}
add_filter('generate_rewrite_rules', 'taxonomy_rewrite_fix');
The key is to replace "paged" to "page" into the rewrite rule for your custom taxonomy.
This is my first contribution here. Hope I help you.

Once I have faced problem about this and passed hard times hrs to hrs by pulling hair. I googled and didn't found any specific solution about the topics. I found several talents' article but they weren't satisfying my problems. Actually custom taxonomy archive page pagination is depends on some settings of arguments of related functions. So I am actually going here sharing my thoughts of solving the taxonomy archive pagination problem.
Five things you need for custom taxonomy archive page pagination working perfectly :
( 1 ) Don't put exclude_from_search parameter key as register_post_type argument parameter or
if mention set it 'exclude_from_search' => false. By default it is set false if not mentioned.
( 2 ) The taxonomy that will be use with the custom post type set
'taxonomies' => 'custom_taxonomy_name' as register_post_type argument parameter or
use register_taxonomy_for_object_type() directly.
Custom taxonomies still need to be registered with register_taxonomy().
( 3 ) While querying within new WP_Query ($args)
i ) If not set in admin `static front page` use before `new WP_Query($args)`
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
and use $query = new WP_Query( array( 'paged' => $paged ) );
ii ) If set in admin static front page use before 'new WP_Query($args)'
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
and use $query = new WP_Query( array( 'page' => $paged ) );
Remember to use posts_per_page and paged parameter in new WP_Query($arg) argument array.
If not set static front page then you should use page parameter in new WP_Query ($arg) argument array
( 4 ) Use Wordpress paginate_links( $args ) function like the example below to render pagination in archive template file.
<?php $big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%', or '/paged=%#%', // if using pretty permalink
'current' => max( 1, get_query_var('paged') ),
'total' => $query->max_num_pages ) ); // Here $max_num_pages is the properties of new WP_Query() object . It is total number of pages. Is the result of $found_posts / $posts_per_page
?>
( 5 ) The paginate_links() function output the ul li listing with page-numbers class.
If you use bootstrap inject pagination class to the ul with the help of javascript or jquery and a nice fancy pagination will be output.
Hope you can now enjoy pagination in the taxonomy archive template without any 404 problem :-)

What helped me is to set "Blog pages show at most" in Reading Settings to 1. Then it works.
With the default 10, it throws 404 error on page 2. On page 1 it's all perfect.
So given this works, the solution is to set "Blog pages show at most" to 1 only for those taxonomies or categories you need. The code you should place inside functions.php is here: https://forums.envato.com/t/wordpress-custom-page-type-taxonomy-pagination/76549/12

Basically that's
get_query_var('page')
that holds the page number
from the working code:
global $query_string;
$page_index = max(1, get_query_var('page'));
parse_str($query_string, $queryParams);
$queryParams['paged'] = $page_index;
$queryParams['posts_per_page'] = $posts_per_page;
query_posts($queryParams);

Use query_posts instead of wp_query. It should work.
This is my Code.
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'paged' => $paged,
'orderby' => 'asc',
'tax_query' => array(
array(
'taxonomy' => "$term->taxonomy",
'terms' => "$term->name",
'field' => 'slug'
)
)
);
$query = new WP_Query( $args );
if($query->have_posts() ) :
// your code
?>
<?php endwhile; endif;
wp_pagenavi();?>

When you create any CPT then you need to add this function.flush_rewrite_rules();It will manage/adjust/reassign your memory.So please add this line your code will work.Precaution.1: Add this function : flush_rewrite_rules();2:Your pagination function (wp_pagenavi()) will call just below the endwhile;I 'm sure,your pagination will work and let me know if it will not work.
Thanking you.

Related

Wordpress Show custom post type specific category in post page?

I am new in development. I have created a custom post type manually, which is as in the code given below. I am unable to get post form specific category (Logo, Website) in Custom post type. Help me to solve this.
function my_custom_taxonomies(){
$lables = array(
'name' => 'Type',
'singular_name' => 'my_custom_taxonomiesType',
'all_items' => 'All Types',
'add_new_item' => 'Add Type',
'edit_item' => 'Edit Type',
'search_item' => 'Search Type',
'parent_item' => 'Parent Type',
'parent_item_colon' => 'Parent Type',
'update_item' => 'Update Type',
'new_item_name' => 'New Type Name',
'menu_name' => 'Type'
);
$args = array(
'labels' => $lables,
'query_var' => ture,
'rewrite' => array('slug' => 'type'),
'hierarchical' => true,
'show_ui' => true,
'show_admin_column' => true,
);
register_taxonomy('type', array('portfolio'), $args);
}
add_action('init','my_custom_taxonomies');
I am trying this code, but cannot get it to work.
$args = array( 'post_type' => 'portfolio', 'category_name' => 'logo', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<ul>
<li><?php the_title(); ?></li>
</ul>
<?php echo '<div class="entry-content">';
//the_content();
echo '</div>';
endwhile;?>
Just Use this as you have registered taxonomy. You can refere about WP_Query here
$query = new WP_Query( array(
'post_type' => 'portfolio',
'tax_query' => array(
array (
'taxonomy' => 'type',
'field' => 'slug',
'terms' => 'logo',
)
),
) );

How to set taxonomy in custom plugin?

I am trying to create a plugin, I need a custom post and taxonomy. But it can be accessible in admin side, but the taxonomy is not working on front-end.
This is how I registered the custom post and taxonomy:
function post_type_questionnaire()
{
$labels = array(
'name' => _x('Questionnaire', 'post type general name'),
'singular_name' => _x('Questionnaire', 'post type singular name'),
'add_new' => _x('Add New Question', 'questionnaire'),
'add_new_item' => __('Add New Questionnaire')
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'_builtin' => false, // It's a custom post type, not built in!
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title',
//'editor',
/*'excerpt',
'thumbnail',
'trackbacks',
'custom-fields',
'comments',
'revisions',
'author',
'page-attributes'*/
));
register_post_type('questionnaire',$args);
}
add_action('init', 'post_type_questionnaire');
function create_questionnaire_taxanomies(){
register_taxonomy('qcategories','questionnaire', array(
'hierarchical'=>true,
'label'=>'Questionnaire Categories',
'rewrite' => array( 'slug' => 'questionnaire' )
));
}
add_action('init', 'create_questionnaire_taxanomies',0);
I am using a shortcode to display it in front-end.
by using the code below it shows all posts
$args = array(
'post_type' => 'questionnaire',
'posts_per_page' => -1,
);
$query = new WP_Query($args);
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) :query->the_post(); ?>
But when I try to specify a taxonomy term it doesn't work and this is the code:
$args = array(
'post_type' => 'questionnaire',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'qcategories',
'field' => 'slug',
'terms' => $atts["name"]
)
)
);
$query = new WP_Query($args);
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
When I tried to display the count of post in a template page it show nothing:
$term = get_term( 3, 'qcategories' );
echo $term->count;
This outputs nothing, so I believe that the taxonomy is not registering, can anybody help me to register the taxonomy in my custom plugin. Thanks in advance!
Edited
This is the output of $args:
array (size=3)
'post_type' => string 'questionnaire' (length=13)
'posts_per_page' => int -1
'tax_query' =>
array (size=1)
0 =>
array (size=3)
'taxonomy' => string 'qcategories' (length=11)
'field' => string 'slug' (length=4)
'terms' => string 'new' (length=3)
Looked like the code was just fine, as OP discovered there was just a little oversight in the code i.e in the plugin before registering taxonomy there was WordPress template tag if(is_admin()) which resulted the code work just fine in the Admin Panel, however when it was called in the front-end , there was no Taxonomy available for the WP_QUERY
Removing that admin_only if condition, code will just work fine.

wordpress shortcode how to filter according to a custom taxonomy?

I have created a custom post type (school) and a custom taxonomy (region) attached to it.
Everything works great except I want to create a shortcode that only get the school that belongs to a specific entry in that custom taxonomy.
That means when you will write the shortcode you will pass a region name, and only schools that belong to that region will return from the database.
I managed to use the shortcode, but not to filter according to the region.
This is my custon taxonomy:
$taxonomies['regions'] = array(
'hierarchical' => true,
'query_var' => 'school_region',
'rewrite' => array(
'slug' => 'schools/region'
),
'labels' => array(
'name' => __('Region'),
'singular_name' => __('Region'),
'add_new' => __('Add New Region'),
'add_new_item' => __('Add New Region'),
'edit_item' => __('Edit Region'),
'new_item' => __('Add New Region'),
'view_item' => __('View Region'),
'search_items' => __('Search Regions'),
'not_found' => __('No Region Found'),
'not_found_in_trash' => __('No Regions Found In Trash'),
),
);
And this is my shortcode:
add_shortcode('rs_school',function($atts, $content=null){
$loop = new WP_Query( array(
'post_type' => 'rs-school',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
) );
//var_dump($loop);
if($loop->have_posts() ){ ?>
<ul class="related">
<?php
while ($loop->have_posts()) {
$loop->the_post();
//$meta = get_post_meta(get_the_ID(), '');
$terms = get_the_terms( $post->ID , 'regions' );
var_dump($terms);
if (in_array($theregion, $terms)){
?>
<li>
<?php echo get_the_title() ?>
</li>
<?php
}
} ?>
</ul>
<div class="clearfix"></div>
<?php
}
});
How can I do that?
best regards
so managed to work it out. so hopefully will help others:
i send the term id to the short code, and use it to fillter it:
$atts = shortcode_atts(
array(
'region' => 15,
), $atts, 'rs_school' );
and then filter it like so:
$loop = new WP_Query( array(
'post_type' => 'rs-school',
'posts_per_page' => -1,
'orderby' => 'date', // Purely optional - just for some ordering
'order' => 'DESC', // Ditto
'tax_query' => array(
array(
'taxonomy' => 'regions', //or tag or custom taxonomy
'field' => 'id',
'terms' => $atts['region']
)
)
) );
hope it will help others.

Ordering custom post types

I'm registering a new custom post type like this:
function news_register() {
$labels = array(
'name' => _x('News', 'post type general name'),
'singular_name' => _x('News Item', 'post type singular name'),
'add_new' => _x('Add New News Article', 'resources item'),
'add_new_item' => __('Add New News Article'),
'edit_item' => __('Edit News Article'),
'new_item' => __('New News Article'),
'view_item' => __('View News Article'),
'search_items' => __('Search News Articles'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 8,
'supports' => array('title','editor', 'excerpt', 'thumbnail', 'excerpt')
);
register_post_type( 'news' , $args );
}
add_action('init', 'news_register');
And I'm then trying to query those post types and order them by title using:
$news = new WP_Query(array(
'post_type' => 'news',
'orderby' => 'name',
'order' => 'DESC'
));
Or:
$args = array( 'post_type' => 'news', 'posts_per_page'=>5, 'orderby'=>'title','order'=>'ASC');
$news = new WP_Query( $args );
Neither of which order by the post title at all. The news posts are test articles that are called 'aaa', 'bbb' and 'ccc.
How can I change my query to get the posts by title order?
EDIT:
I have this code in functions, but removing it doesn't seem to change anything.
add_filter( 'pre_get_posts','change_my_post_order' );
function change_my_post_order( $query ) {
global $wp_query;
if ( is_category() ) {
$query->set( 'orderby', 'title' );
$query->set( 'order', 'asc' );
}
return $query;
}
Adding "'suppress_filters' => true," to the query doesn't seem to make any difference either.
Your pre_get_posts action is wrong. You should make sure that you only target the front end and only the main query. Your action should look like this
add_action( 'pre_get_posts','change_my_post_order' );
function change_my_post_order( $query ) {
if ( !is_admin() && $query->is_main_query() && $query->is_category() ) {
$query->set( 'orderby', 'title' );
$query->set( 'order', 'asc' );
}
}

Wordpress paginate_links() function, 404s and trouble

This has been a horrendous, crippling, time stealing (two week) horrible horrible problem.
It might be rewriting, my use of custom posts, but after stripping out so much (and reducing some functionality of my theme) I've reduced my problem to this:
paginate_links() is putting out a link like this:
?s=cute&post_type=image&paged=2
When I changed the variable in the browser bar to page=2 (dropping the 'd').
?s=cute&post_type=image&page=2
It works correctly.
So my question reduced to this: If I must get that function to output the "page" variable properly, how is that done?
paged and paged are both used by WordPress. So conversely, how do I get paged recognized if that is better practice?
For all I know, this is indicative of some deeper issue I have in my theme, but for the life of me I can't see how else I could be going wrong!
EDIT:
Here is my code I'm using:
if ( get_query_var('paged') )
$paged = get_query_var('paged');
elseif ( get_query_var('page') )
$paged = get_query_var('page');
else
$paged = 1;
$big = 999999999; // need an unlikely integer
$stuff_to_echo = paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?page=%#%',
'current' => max( 1, $paged ),
'total' => $wp_query->max_num_pages,
'type' => 'array'
) );
EDIT 2 - If above is good, here is another possible area the problem might be happening. I'll post the creation of the custom post type and the rewrite rules -
//image custom post type
add_action( 'init', 'symbiostock_image_manager_register' );
function symbiostock_image_manager_register( )
{
//creating custom post type for image
$labels = array(
'name' => 'Symbiostock Images',
'singular_name' => 'Image',
'add_new' => 'New Image',
'add_new_item' => 'Add New Image',
'edit_item' => 'Edit Image',
'new_item' => 'New Image',
'all_items' => 'All Images',
'view_item' => 'View Image',
'search_items' => 'Search Images',
'not_found' => 'No image found',
'not_found_in_trash' => 'No images found in Trash',
'parent_item_colon' => '',
'menu_name' => 'RF Images'
);
$args = array(
'labels' => $labels,
'singular_label' => __( 'Image' ),
'description' => 'Image Listings',
'menu_position' => 100,
'menu_icon' => symbiostock_IMGDIR . '/symbiostock_icon2.png',
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => true,
'has_archive' => true,
'exclude_from_search' => false,
'supports' => array(
'title',
'editor',
'thumbnail'
),
'rewrite' => array(
'slug' => 'image',
'with_front' => false
)
);
register_post_type( 'image', $args );
register_taxonomy( 'image-type', array(
'image'
), array(
'hierarchical' => true,
'label' => 'Image Categories',
'singular_label' => 'Image Type',
'rewrite' => true,
'exclude_from_search' =>false,
'public' => true,
'slug' => 'image-type'
) );
register_taxonomy( 'image-tags', array(
'image'
), array(
'hierarchical' => false,
'rewrite' => true,
'query_var' => true,
'singular_label' => 'Image Keyword',
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'slug' => 'image-tag',
'labels' => array(
'name' => _x( 'Image Keywords', 'taxonomy general name' ),
'singular_name' => _x( 'Keywords', 'taxonomy singular name' ),
'search_items' => __( 'Search Images' ),
'all_items' => __( 'All Image Keywords' ),
'edit_item' => __( 'Edit Image Keyword' ),
'update_item' => __( 'Update Image Keyword' ),
'add_new_item' => __( 'Add New Image Keyword' ),
'new_item_name' => __( 'New Keyword Name' ),
'menu_name' => __( 'Image Keywords' ),
),
'rewrite' => array(
'slug' => 'search-images', // This controls the base slug that will display before each term
'with_front' => false,
'hierarchical' => false
),
) );
}
add_action( 'admin_init', 'symbiostock_image_directory' );
function symbiostock_image_directory( )
{
add_meta_box( 'symbiostock-image-meta', 'Symbiostock Image Info', 'symbiostock_image_manager_meta_options', 'image', 'normal', 'high' );
}
And here are the rewrite rules further down -
add_action( 'init', 'symbiostock_rewrite' );
function symbiostock_rewrite( )
{
global $wp_rewrite;
$wp_rewrite->add_permastruct('typename','typename/%year%%postname%/' , true , 1);
add_rewrite_rule('typename/([0-9]{4})/(.+)/?$','index.php?typename=$matches[2]', 'top');
$wp_rewrite->flush_rules();
}
I know this is old but I've had the same problem and solved it by changing the permalink of the page that was causing the 404.
This is because, apparently, you cannot have a page-slug with the same name as your custom post type.
All credit goes to Ryan S. for sharing the original solution: http://www.sutanaryan.com/2013/09/404-error-in-custom-post-type-pagination-wordpress/
Under your args for paginate_links() I would ensure that you have your format set properly. 'paged' is typically used for obtaining the "current page" the user is on (Reference: https://codex.wordpress.org/Pagination#Adding_the_.22paged.22_parameter_to_a_query)
'format' => '?page=%#%'
it sounds like you have it set to '?paged=%#%'
Here is a complete example
global $wp_query;
$args = array(
'format' => '?page=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
);
paginate_links($args);
It might not be the most elegant solution, but is appropriate in this case.
I simply set up a redirect to the taxonomy page in the event someone is searching this post type.
add_action( 'parse_query', 'image_search_redirect' );
function image_search_redirect( $query ) {
if ( ( is_search() && get_query_var( 'post_type' ) == 'image' ) ) {
wp_redirect(home_url("?image-tags=") . urlencode(get_query_var('s')));
exit();
}
}
This is not as much a solution as a creative work-around. There should be no reason why I get a 404 not found on the "paged=" variable. If some future person knows a better solution I'd love to know one!

Resources