1

Our customer wants a "Latest Newsletter" type link on the homepage.

I figured there'd be an easy way to specify a document from the media library (perhaps by name or title) but nothing is coming up in Google.

So he'll be uploading jan.doc, then feb.doc etc to the media library. He doesn't want to keep overwriting the same one, as it's neat to build up a little library of these things in his WordPress dashboard.

But I think he'd be prepared to flag or tag the latest one, or even rename the latest one each month to "Latest Newsletter" as its title in Wordpress (not the filename).

But how can our theme's header reliably link to his chosen/latest newsletter?

  • Please check out for example: WP_Query() and even register_post_types() to register a custom newsletter post type where you can attach the documents. Try it out, post your code and explain where it fails. Otherwise your question is too much of "work for me for free" ;-) – birgire Feb 8 '14 at 9:55
  • 1
    Another way would be to consider media tags. Did you try out that possibility? – birgire Feb 8 '14 at 10:07
  • @birgire not asking you to work for free. if you say there's no other way but to code my own query which examines the media library, then sorts it then I am happy to have that as an answer and to accept it. i'm not super hot with PHP but i can do that. however i didn't want to do that if there's already a cleaner and simpler way to do it in Wordpress that i didn't know about. often in the past i've assumed i'll have to code up something up from scratch in Wordpress only to discover later that a better solution is available readymade and for free, within the wordpress ecosystem – hawbsl Feb 24 '14 at 15:02
2
+50

You could search the latest attachments like my example below. I searches for the string "newsletter", and the first time it pops up in a filename, it writes a link. I've tried to make it easy to understand.

$attachments = get_posts( array(
    'post_type' => 'attachment',
    'posts_per_page' => 20,
    'post_status' => null,
    'post_mime_type' => 'application/pdf,application/msword'
) );

foreach ( $attachments as $attachment ) {
    $url = wp_get_attachment_url( $attachment->ID);
    $needle = "newsletter";
    if(stripos($url, $needle) !== false){
        echo "<a href='$url'>";
        echo "Read the latest newsletter here";
        echo "</a>";
        break;
    }
}

posts_per_page sets how many attachments you want to search, so it depends on how much activity the site has got, and how far back it should look for a file matching the credentials.

The only BUT in this is that you should have a specific string represented in every newsletter name uploaded in order for this to work. So newsletter_feb.doc would work in this example.

Note that you should specify post_mime_type to match the documents your client will be uploading newsletters in. But like this it skips images, which in my experience fills up a lot of space in the media library.

  • thanks @Jeppe, i've been coding up something similar ... but shouldn't we apply an OrderBy parameter to the get_posts? – hawbsl Feb 24 '14 at 17:45
  • 1
    Since post_date is the default orderby, order is DESC by default and you wish to get the newest file containing a specific filename, you should be fine using this code. – Jeppe Feb 24 '14 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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