I have a wordpress query that outputs posts that contain the following info: - year - link to a pdf-file - title - post image

so its essentially an archive for yearly reports dating back to the 1860's until today. For the early days there arn't pdf-files/posts for every year. in the newer days in some years there are multiple files per year.

i would need the query to output an UL with the following configuration:

<li class="decimal-year pdf-present"><a href="#">1860</a><div>Image, Title, PDF Link in Post</div></li>
<li class="normal-year pdf-present"><a href="">1861</a><div>Image, Title, PDF Link in Post</div></li>
<li class="normal-year no-pdf"><a href="#">1862</li>

so the class should contain info if its "decimal" year like 1860, 1870, 1880, etc. or a year inbetween like 1861, 1862, 1863, etc. Then there needs to be a class noting if theres a corresponding yearly post or not. (the links will be displayed on a visual timeline) so even if there is no yearly output, there still needs to be a li element. if there are multiple files in a year there should be another ul in the div containing the data.

thats quite a query i know!

heres the query so far:

// set the query
        $query = array (

        'post_type' => 'jahresberichtpdf',
        'orderby' => 'jahresberichtjahr'
        // loops
        $queryObject = new WP_Query($query);
        if ($queryObject->have_posts()) : ?>
        <?php while ($queryObject->have_posts()) : ?>
        <?php $queryObject->the_post(); ?>

        Title:<?php the_title(); ?>
        Thumbnail:<?php the_post_thumbnail('thumbnail'); ?> 

        <strong>PDF:</strong> <?php print_custom_field('jahresberichtfile:to_link'); ?>
        <strong>Year:</strong> <?php print_custom_field('jahresberichtjahr'); ?>

        <?php endwhile; ?>
        <?php endif; ?>

has anybody done a query like this or would be up for the challenge? if someone is willing to put some effort in, i would show my appreciation in form of a paypal donation!

thanks, mark

  • It doesn't look like much of a query at all to me. It looks like you just need to Loop over the results and conditionally add the markup. What am I missing?
    – s_ha_dum
    Commented May 26, 2013 at 14:19
  • it might be me missing something :-) so how would i fill the "empty" years with li elements and add classes depending on years?
    – Mark Nolan
    Commented May 26, 2013 at 14:29

1 Answer 1


Assuming this to be the primary question:

How would i fill the "empty" years with li elements and add classes depending on years?

You don't really have a "query" problem, just a PHP/markup one. You need to track you years independently of your query. Below is proof-of-concept code for doing that. I did not try to make it output the precise markup you require. Be aware that the Loop is very sensitive to post order and that you need 'ignore_sticky_posts' => true or post dates will be out of order and the loop will not work correctly. It will stop at the year of the last post, not necessarily at "this year".

$year = 2000;
$qry = new WP_Query(
    'ignore_sticky_posts' => true

if ($qry->have_posts()) {
  while ($qry->have_posts()) {
    echo $year;
    if (date('Y',strtotime($qry->post->post_date)) == $year) {
    } else {
      echo 'no posts';
    echo '<br />';
  • would this do a query to the database for every year?
    – Mark Nolan
    Commented May 27, 2013 at 9:14
  • It does not do an individual query per year. That would be a huge load on the server. It pulls all of your posts for that post type and prints them out by year, including "empty" years. Did you try it?
    – s_ha_dum
    Commented May 27, 2013 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.