Pagination and Order BY

Asked

Viewed 112 times

0

I have noticed that some sites in wordpress use in the links the following code

?orderby=title&order=asc

Could you explain to me the variations of this code?

For example, it would be possible to sort articles by letter as follows.

?orderby=title&order=asc letter A : shows articles with letter A

?orderby=title&order=asc number of posts : shows 20 articles

1 answer

0

These "variations" you see are the parameters of WP Query. In the link you can see all the (many) possible combinations that can be made. Answering your questions in particular:

Show articles with letter A

Articles containing the letter A, in whichever article point? Articles whose title initiating with the letter A?

Assuming it is the last one, I believe you have to make some sort of filter after the consultation. I don’t know if this is possible just by using the query parameters. You can do something like this:

$args = array(
    'orderby' => 'title',
    'order' => 'ASC',
    'posts_per_page' => -1 //retorna todos os posts da base
);

query_posts($args);

if (have_posts()) { 

    $letra = 'A';

    while (have_posts()){
        the_post();
        $letraInicial = strtoupper(substr($post->post_title,0,1));
        if ($letraInicial == $letra) {
            // coisas do loop, como the_title() e etc
        }
    }
}

This code is far from great, but I believe it works as proof of concept. Basically, it searches all the posts from your base, and compares the first letter of each of them with a letter that you previously established. For more complex or even more performative queries, you can play with the $wpdb.

Show 20 articles

There is a proper parameter for this:

$args = array(
    'orderby' => 'title',
    'order' => 'ASC',
    'posts_per_page' => 20 //retorna o número de posts desejados
);

In query string format:

/?orderby=title&order=asc&posts_per_page=20

Browser other questions tagged

You are not signed in. Login or sign up in order to post.