Paging and Order BY

0

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

? orderby = title & order = asc

Could you explain 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: show 20 articles

    
asked by anonymous 02.08.2015 / 18:45

1 answer

0

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

  

Show articles with the letter A

Articles that contain the letter A, in any point of the article? Articles whose title starts with the letter A?

Assuming it's the last, I believe you have to do some sort of filter after the query. I do not know if this is only possible using 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 fetches all posts from its base, and compares the first letter of each of them with a letter that you establish previously. For more complex or even more performative queries, you can play around with $ wpdb .

  

Show 20 articles

There is a proper parameter to 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
    
03.08.2015 / 14:20