Forums

get_posts assistance needed (5 posts)

  1. candlebain
    Member
    Posted 1 year ago #

    I'm trying to use get_posts() to get a list of attachments for a College Class sub-page I've built. To organize things, I'm using Categories to identify posts dealing with the College Class, and then I've built a Custom Category Template for it.

    Here's the problem:

    get_posts() doesn't appear to be accepting the 'category_name' argument. I may be using it wrong, so here's my code:

    <?php
    
    $args = array(
    	'post_type' => 'attachment',
    	'category_name' => 'rock-history',
    	'numberposts' => -1,
    	'post_status' => null,
    	'post_parent' => null, // any parent
    	);
    $attachments = get_posts($args);
    if ($attachments) {
    	foreach ($attachments as $post) {
    		setup_postdata($post);
    		the_title();
    		the_attachment_link($post->ID, false);
    		the_excerpt();
    	}
    }
    
    ?>

    Without the 'category_name' argument, everything shows from the entire site. With it, nothing shows...including attachments from posts that have the category tag applied.

    Do Slugs need to be done with any special formatting? Any suggestions on how to get this to work?

    Thanks in advance.

  2. lindsayanng
    Member
    Posted 1 year ago #

    This is generally what I use to pull posts from a specific category
    <?php query_posts('showposts=1&cat=3') ?>

  3. candlebain
    Member
    Posted 1 year ago #

    Any tips on getting this to work though? Am I using it wrong?

    I actually need the attachments in a single page as a repository for attachments from all posts related to a class. I don't need to post text on this page. I'm just trying to pull all attachments from only the College Class category into a single page. Instead, I'm either getting every attachment from every category, or nothing at all.

  4. candlebain
    Member
    Posted 1 year ago #

    Is it possible the slug is only associated with the text? Or is it associated with the media as well?

    Any idea how I might do this?

  5. candlebain
    Member
    Posted 1 year ago #

    Here's something else I've tried:

    <?php
    	$args = array(
    				'post_type' => 'attachment',
    				'post_mime_type' => 'application/pdf'
    			);
    	$pdfs =& get_children($args);
    	print_r($pdfs);
    	foreach ( (array) $pdfs as $attachment_id => $attachment ) {
    		echo wp_get_attachment_link( $attachment_id );
    	}
    ?>

    It's not recognizing my MIME type. I was hoping this would let me sort into categories once I found the links...

    Ugh.

Topic Closed

This topic has been closed to new replies.

About this Topic