Wordpress Taxonomy listing - Pagination - php

I found this excellent function to display all the posts listed under a specific custom taxonomy. It works great. Found here http://gilbert.pellegrom.me/wordpress-list-posts-by-taxonomy I tried a number of ideas, however unsuccessful, to try and paginate the returned data. I either get no data or it continues to display the entire list. Some of my taxonomies have over 10K posts associated. So pagination would seem logical.
What I want to do is; have the information that gets returned create pages of 'n' number of posts and make links for the other pages (1,2,...4,5 etc). Any help is greatly appreciated.
I tossed this in my functions file;
function list_posts_by_taxonomy( $post_type, $taxonomy, $get_terms_args = array(),
$wp_query_args = array() ){
$tax_terms = get_terms( $taxonomy, $get_terms_args );
if( $tax_terms ){
foreach( $tax_terms as $tax_term ){
$query_args = array(
'post_type' => $post_type,
"$taxonomy" => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => -1,
'ignore_sticky_posts' => true
);
$query_args = wp_parse_args( $wp_query_args, $query_args );
$my_query = new WP_Query( $query_args );
if( $my_query->have_posts() ) { ?>
<h2 id="<?php echo $tax_term->slug; ?>" class="title">
<?php echo $tax_term->name; ?></h2>
<ul>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark"
title="Permanent Link to <?php the_title_attribute(); ?>">
<?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
<?php
}
wp_reset_query();
}
}
}
?>
And this code goes in the template, stuff whatever 'taxonomy' name and it displays the data. Another questions I was not sure about, if the pagination should go in the function or the template.
<div class="my_class">
<?php
list_posts_by_taxonomy( 'my_posttype', 'taxo_mytaxo' );
?>
</div>
Thank you everyone!

In order to have pagination with your query first of all you have to use the paged parameter.
To get some extra info check the wordpress codex for Pagination
Usually you get the pagination variable like this:
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
Then you pass it to the query by including it in the query arguments for your code:
$query_args = array(
'post_type' => $post_type,
"$taxonomy" => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => -1,
'ignore_sticky_posts' => true
'paged' => $paged //I've added it here
);
Then you'll have to build the pagination links something like(this will be done inside the loop):
<!-- Add the pagination functions here. -->
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>

Related

I'm struggling with Wordpress and Advanced Custom Fields

I've got some PHP code which I'm using in a WordPress website. Here's the code:
<h3>Case Studies</h3>
<?php
$the_query = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => -1,
'cat' => 3,
'meta_key' => 'sector',
'orderby' => 'meta_value',
'order' => 'ASC'
));
if( $the_query->have_posts() ):
while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php echo get_permalink(); ?>">
<h1><?php the_field('client_name'); ?></h1><p><?php the_field('sector'); ?></p>
<span style="background-image:url(<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); echo $url; ?>)"></span>
</a>
<?php endwhile; endif; ?>
<h3>Other Clients</h3>
<?php if( have_rows('clients') ):
while ( have_rows('clients') ) : the_row(); ?>
<a>
<h1><?php the_sub_field('client'); ?></h1><p><?php the_sub_field('sector'); ?></p>
<span></span>
</a>
<?php endwhile; endif; ?>
So - at the top, we've got "Case Studies" and this is just pulling in some details from the posts on the website (the client name and the sector).
Next, I've got "Other Clients" - this is set up as an Advanced Custom Field on the page that this code appears on. Pretty simple as well.
Now, here's the fun:
It works if I reverse the two sections ("Other Clients" first) but not this way - any ideas what's going wrong? I'm presuming it's something in the "Case Studies" section that's messing up the following one, but I'm at a loss. If I can provide any more info, let me know!
Much thanks in advance x
After finishing your custom query you must call wp_reset_postdata();
$the_query = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => -1,
'cat' => 3,
'meta_key' => 'sector',
'orderby' => 'meta_value',
'order' => 'ASC'
));
if( $the_query->have_posts() ):
while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php echo get_permalink(); ?>">
<h1><?php the_field('client_name'); ?></h1><p><?php the_field('sector'); ?></p>
<span style="background-image:url(<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); echo $url; ?>)"></span>
</a>
<?php endwhile; endif; ?>
<?php wp_reset_postdata(); ?>
Because your custom query $the_query->the_post(); overrides global $post object, after finishing your query must always do this.

Wordpress query with two custom taxonomies

Been stuck on this for days, so I thought I'd reach out.
I've created two custom taxonomies, singleclient and dualclient. These are populated with special content via Advanced Custom Fields.
What I'm looking to do is create a custom page template which joins these two taxonomies and prints out content from the two custom taxonomies.
This works for me for one of the taxonomies:
<?php
$args=array(
'post_type' => 'singleclients',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php the_field('client1_img'); ?>
<h3><?php the_field('client1_name'); ?></h3>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
I've tried following what the Wordpress Codex says to do, but I can't seem to get it working. Here's an example which I thought would work, but displays nothing at all.
<?php
$args=array(
'post_type' => 'post',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'singleclients',
'field' => 'slug',
'terms' => 'singleclients'
),
array(
'taxonomy' => 'dualclients',
'field' => 'slug',
'terms' => 'dualclients'
)
)
);
$posts = get_posts( $args );
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php the_field('client1_img'); ?>
<p>print test content</p>
<h3><?php the_field('client1_name'); ?></h3>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
Really pretty frustrated at this point. Anyone know what I am doing wrong and why it won't print out both taxonomies?
I did this on a website a while back:
//queries
$query1 = new WP_Query($arg1);
$query2 = new WP_Query($arg2);
//create new empty query
$wp_query = new WP_Query();
$wp_query->posts = array_merge( $query1->posts, $query2->posts );
You can now access your posts as you normally would.
I am not very good at explaining stuff, so let me know if you need more info...
I couldn't get this to work, unfortunately.
What I ended up doing was creating one single taxonomy called "clients" and enabling categories for that taxonomy. This allowed me to assign "children" to clients of the types "singleclient" and "dualclient".
When I called the loop, I called one single taxonomy and then separated the output posts into two types. My code is below. I hope that this helps someone in the future.
<?php
$args=array(
'post_type' => 'clients',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php if ( in_category( 'dualclient' )) { ?>
<!-- show dual client -->
<section class="two columns padding10">
<a href="<?php the_permalink() ?>">
<section class="four columns client1 dualclient_first" style="background-image:url('<?php the_field("dualclient1_img") ?>')"></section>
<section class="four columns client1" style="background-image:url('<?php the_field("dualclient2_img") ?>')"></section>
</a>
</section>
<?php } elseif ( in_category( 'singleclient' )) { ?>
<!-- show single client -->
<section class="two columns padding10">
<a href="<?php the_permalink() ?>">
<section class="eight columns client1" style="background-image:url('<?php the_field("dualclient1_img") ?>')"></section>
</a>
</section>
<?php } else { ?>
<p>Woops.</p>
<?php } ?>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
Have you tried this way?
//First Query
$args=array(
'taxonomy' => 'singleclients',
'posts_per_page' => 1,
);
query_posts($args);
//Get the Posts
get_template_part( 'loop', 'index' );
wp_reset_query();
//Second Query
$args=array(
'taxonomy' => 'dualclients',
'posts_per_page' => 1,
);
query_posts($args);
wp_reset_query();

Get Wordpress posts from two categories

I am trying to display posts from two categories on one page (6 & 7). It should include posts from either category as well as the ones matching both. Unfortunately I have so far only found solutions for the latter which is why I am asking you for help now. This is my current code:
<div class="sponsoren_container_wrapper">
<?php $args = array ( 'posts_per_page' => -1, 'category' => 6 );
$myposts = get_posts ( $args );
foreach ($myposts as $post) : setup_postdata( $post ); ?>
<div class="gallery_image_container">
<a href="<?php
$content = get_the_content();
print $content;
?>" title="<?php the_title_attribute(); ?>">
<div class="gallery_image_thumb">
<?php the_post_thumbnail('thumbnail'); ?>
</div>
<div class="gallery_title">
<h2>
<?php the_title(); ?>
</h2>
</div>
</a>
</div>
<?php endforeach; wp_reset_postdata(); ?>
</div>
How would I be adding the category ID "7" to that code to make everything work as explained above?
Thanks in advance!
use a comma seperated string:
$args = array ( 'posts_per_page' => -1, 'category' => '6,7' );
http://codex.wordpress.org/Template_Tags/get_posts
$args = array ( 'posts_per_page' => -1, 'category' => '6,7' );
$myposts = get_posts ( $args );
$args = array ( 'posts_per_page' => -1, 'category' => '6,7' );
$myposts = get_posts ( $args );
For me, 'category' would not pass but 'cat' did. I hope this helps someone.

On wordpress set a custom number of posts in a category page

I have a wordpress website and in the custom template for the category 10 I have this code:
<?php
function new_excerpt_length($length) {return 30;} add_filter('excerpt_length', 'new_excerpt_length');
global $post;
$args = array( 'numberposts' => 5, 'category' => 10 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
echo get_the_post_thumbnail($post_id, $size)?></div><div id="eu_post_category">
<h2><a class="roll-link" href="<?php the_permalink(); ?>"><span data-title="<?php the_title(); ?>"><?php the_title(); ?></span></a></h2>
<h6><?php the_excerpt(); ?></h6>
<?php endforeach; ?>
This returns the last five posts in my category. But I do not want to lose the older posts. I want to have two buttons newer posts||older posts at the bottom in order to let the user see all the posts.
What I've tried: setting the max number in settings-> reading and to use this code in functions.php (with no results):
function limit_posts_per_archive_page() {
if ( is_category('10') )
set_query_var('posts_per_archive_page', 5); // or use variable key: posts_per_page
}
add_filter('pre_get_posts', 'limit_posts_per_archive_page');
Thanks!
< when u click on new posts/older posts button get the page number(which is stored inside $paged varible) pass this inside the arguments
<?php
$args = array(
'paged' => $paged,
'category' => 10,
'posts_per_page' => 5,
'post_status' => 'publish');
query_posts($args);
if ( have_posts() ) {
while ( have_posts() ) { the_post();
echo $post->post_title;
}
}
?>
for references:
http://codex.wordpress.org/Function_Reference/query_posts http://codex.wordpress.org/Template_Tags/get_posts

Can't filter on category in loop when using Custom Post Type in Wordpress

I'm trying to display posts from a custom post type called portfolio using the code below and then filter the result by category, I have tried putting - category_name , catid, portfolio_category in the array
and have had a look around the forums and tried a few things but cant seem to get it to work , it either displays nothing or all the post from all categories.
<?php
$args=array(
'post_type' => 'portfolio',
'post_status' => 'publish',
'posts_per_page' => 7,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to
<?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
The registered taxonomy is portfolio_category, any help on this would be appreciated , many thanks
You could try to call the the category as a custom taxonomy:
'portfolio_cat' => 'name_of_your_category'
After trying a few things this is how i got it to work using the 'tax_query' hope this helps someone...
<?php
$args = array(
'post_type'=>'portfolio',
'tax_query' => array(
array(
'taxonomy' => 'portfolio_category',
'field' => 'slug',
'terms' => 'solutions'
)
)
);
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to
<?php the_title_attribute(); ?>">
<?php the_title(); ?></a></p>
<?php the_excerpt()?>
<?php echo get_the_post_thumbnail($post->ID, array(50,50)); ?>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>

Categories