• Resolved scottiescotsman

    (@scottiescotsman)


    I have just recently added a page to my wordpress installation page-actor.php, where I intended to list all the actors that I have on my db and with the movie count associated with them. I have successfully done this but to my dismay I cannot paginate or set posts_per_page, Ideally I would want it paginated a letter as a time e.g. a, b, c….

    <?php
    				
    				foreach ( get_terms( array(
    											'taxonomy' => 'actor',
    										) ) as $cat) :
    										?>
    					<div class="col-md-4">
    						<?php $the_query = new WP_Query( array(
    							'post_type' => 'movies',
    							'tax_query' => array(
    								array(
    									'taxonomy' => 'actor',
    									'field' => 'term_id',
    									'terms' => $cat->term_id,
    								)
    							)
    						) );
    						$count = $the_query->found_posts; ?>
    						
    						<?php echo $cat->name; ?> <span style="color: white"><?php echo $count; ?></span>
    						
    					</div>
    				
    				<?php endforeach; ?>

    Any help greatly appreciated.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    So each actor has their own term, and various movie posts are assigned actor terms. That much is apparent. Are actor terms assigned other posts besides movies? If not, there’s no need to query posts to get a count. The count is part of each term object.

    // for example...
    echo "<li>{$cat->name} is in {$cat->count} of our listed movies</li>";

    If you must have only movie counts and the terms are assigned to other posts, it’ll be more efficient to do a specific SQL count query rather than going through the entire WP_Query process. It’ll make a difference on large datasets.

    Just sayin’
    That’s not what you asked about. I gather you do not want pagination by page number 1, 2, 3, etc., but a series of archive pages by the first letter of the term name. All ‘A’ terms on one page. All ‘B’ terms on another. As far as the sort of query run, that’s not precisely “pagination” per se. Thus posts_per_page is not a factor, besides that relates to posts, not terms. Regardless, I think I understand.

    There is no handy WP function or class that will query for terms by first letter AFAIK. You’ll need to compose your own SQL query executed with $wpdb methods. The letter to query for would need to be in the page’s URL somehow. The simplest would be as an URL query argument. An URL like example.com/actor-list/?first-letter=a would retrieve a page named actor-list which is based on a template that runs the necessary SQL query and lists the results.

    Your SQL would query the terms table for terms WHERE name LIKE 'a%'. You would need to join in the taxonomy table in order to only get terms within the actor taxonomy. I’m unsure of the exact SQL query you need, this is what you need to start with at least.

    Thread Starter scottiescotsman

    (@scottiescotsman)

    I got everything you said and I have managed to do a similar search with the post type movies. So I hope it wont be much harder than the actors taxonomy.

    now trying to query with the top 10 actors according to count ?

    Moderator bcworkz

    (@bcworkz)

    Top 10 of all actors? You could use get_terms() for that, specify the orderby argument is ‘count’ and order is ‘DESC’. You’ll actually get all actors in the returned array. Just take the first 10 terms off the top.

    Adding a simple LIMIT 0,10 to the query would limit the results to the top 10, but there is no easy way to accomplish that with get_terms(). You could use WP_Tax_Query::get_sql() and construct your own SQL from the returned parts, or just compose your own SQL from scratch.

    Unfortunately, WP term query functions are not as robust as post queries, so you more likely run up against the need for custom SQL to efficiently get the results you seek. If you are not too familiar with composing SQL queries, it might be a good thing to work towards. It can serve you well.

    I’m not too familiar with SQL myself. I use WP query functions as a “crutch”. I let WP compose its query as close as possible to what I really want. I find out what that query is, then manually modify it for my exact needs. I find modifying something known to work is much more reliable than composing from scratch.

    Thread Starter scottiescotsman

    (@scottiescotsman)

    solved

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘custom pagination’ is closed to new replies.