• Sorry for my bad english, This is my code
    <?php
    $terms = get_terms(‘loai-tin’, array(
    ‘orderby’ => ‘id’,
    ‘order’ => ‘ASC’,
    ‘hide_empty’ => 1
    ));
    foreach ($terms as $term) {
    $args = array(
    ‘post_type’ => ‘tin-bat-dong-san’,
    ‘post_status’ => ‘publish’,
    ‘posts_per_page’ => 10,
    ‘tax_query’ => array(
    array(
    ‘taxonomy’ => ‘loai-tin’,
    ‘field’ => ‘slug’,
    ‘terms’ => $term->slug,
    )
    )
    );
    $query = new WP_Query($args);

    How to add pagination for this loop. Help me please 🙁

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

    (@bcworkz)

    As your code stands now, pagination is difficult because you are running multiple queries. I suggest you combine your term slugs into an array and query for all posts for all of those terms at one time. Then you can use the ‘offset’ argument to get just posts for a specific page.

    One possible problem will be grouping your posts by terms. If ordering by term ID is acceptable, it’s possible to alter the orderby clause of the query through ‘posts_orderby’ filter to wp_term_relationships.term_taxonomy_id.

    Thread Starter crying2812

    (@crying2812)

    @bcworkz
    tks for reply. Can u suggest me about that query or keyword for search , i’m beginner 🙁

    Moderator bcworkz

    (@bcworkz)

    For querying all terms, look here:
    https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
    Note in the multiple taxonomy example how an array is used to query for multiple terms, the first taxonomy by slug, the other by ID. Of course you do not have multiple taxonomies, so you would have a single array in ‘tax_query’ and no ‘relation’.

    You would construct the term array in your loop, then afterwards construct your query arguments and create a new query object. Then run another loop to output the results.

    posts_orderby filter is here:
    https://developer.wordpress.org/reference/hooks/posts_orderby/
    The page is of little help, but it links to the source code which is what you need to see in order to use filters. More on hooking filters and actions in the Plugin API page.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Pagination cannot work in loop order by term id’ is closed to new replies.