WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Loop for custom taxonomy (15 posts)

  1. Morgan Kay
    Member
    Posted 2 years ago #

    I have a custom post type (portfolio) with a custom taxonomy (genre) which is hierarchical. I'm trying to write a loop that displays all of the portfolios within a given genre. I have been working on this for days, and I can't get anything to work. It seems like no matter what code I try, it always displays all of the portfolios in all of the genres, instead of being limited to one genre.

    Here's my latest version:


    <?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ), OBJECT, $term->name);
    ?>
    <h2 class="genre-title"><?php
    printf( __( '%s', 'comicpress' ), '<span>' . $term->name . '</span>' );
    ?></h2>

    <div id="genre-content">
    <div id="genre-thumbnails">
    <?php $args = array( 'post_type' => 'portfolio', 'genres' => 'atomic-war-1' );
    $loop = new WP_Query( $args );
    ?>
    <?php
    while ( $loop->have_posts() ) : $loop->the_post(); ?>

    <div class="portfolio-item">
    <?php the_post_thumbnail('thumbnail'); ?>
    <div class="entry-content">
    <h3 class="entry-title"><?php the_title(); ?></h3>
    <?php the_content(); ?>
    <?php the_meta(); ?>

    <?php echo get_the_term_list( $post->ID, 'genre', '<p>Genre: ', ', ', '</p>' ) ?>

    </div><!-- .entry-content -->
    </div><!-- .portfolio-item -->
    <?php endwhile;?>
    </div><!-- #genre-thumbnails -->
    </div><!-- #genre-content -->

    This loop shows all of the portfolios in all genres, but I just want it to show portfolios in the genre "atomic-war-1." Actually, eventually I want it to show all portfolios in the genre $term->name, but I'm trying to start out simple.

    Any suggestions?

  2. security_man
    Member
    Posted 2 years ago #

    try get_posts instead

    $myposts = get_posts('post_type=portfolio&genres=atomic-war-1');
    
    foreach($myposts as $post) : setup_postdata($post);
    
    do your loop stuff here
    
    endforeach;
  3. Morgan Kay
    Member
    Posted 2 years ago #

    Thanks, Security Man!

    Unfortunately, that is still doing the same thing - it's showing all posts from all genres.

    Something seems seriously broken here...

  4. security_man
    Member
    Posted 2 years ago #

    that should work... if it is not then odds are you dont have the custom taxonomy correct. maybe genre instead of genres or atomic-war instead of atomic-war-1 ? Not sure on specifics but i will be willing to bet dollars to donuts that that is where the issue is because wordpress is just ignoring the genres parameter so it doesnt understand it somehow.

  5. Morgan Kay
    Member
    Posted 2 years ago #

    I agree that it should work, which is why this has been so incredibly frustrating for me. "genres" instead of "genre" totally breaks the page. I tried with another genre called "comics" to eliminate the potential problems with hyphens and numbers, and I get the same result.

    At this point I think the problem isn't my query or loop, because I have tried a dozen things that ought to work and nothing does. Where else should I be looking for the problem?

  6. security_man
    Member
    Posted 2 years ago #

    not really sure... i mean if it is pulling the custom post type but not filtering by the custom taxonomy then the only thing i can think it would be is the taxonomy portion of the query.

    When you say it breaks the page, how is it broken? do you get an error and if so what is it?

    I take it you didnt create the custom post type or the custom taxonomy, correct? the reason i ask is that the genre instead of genres suggestion was not a specific suggestion but rather so you would check the details of when the custom post type/taxonomy were created to make sure those calls are correct.

    if you want to find that, the function you are looking for is register_post_type... it will usually be called within another function that is used in an add_action to the init. there is an array that has to be passed into register_post_type which has things like the name, singular_name and other parameters that it needs. those labels have to match when you are making queries.

    I hope that makes sense because unless i am missing something there really isnt any other thing that could be the problem.

  7. security_man
    Member
    Posted 2 years ago #

    looking at your code again, can you give me a print_r of the $term object? maybe that will help.

  8. Morgan Kay
    Member
    Posted 2 years ago #

    I created the custom post type and the taxonomy using the wonderfully helpful generators at http://themergency.com/generators/wordpress-custom-post-types/

    Here's the code for them:

    [Code moderated as per the Forum Rules. Please use the pastebin]

  9. security_man
    Member
    Posted 2 years ago #

    hell, try this and see if it works:

    $myposts = get_posts('post_type=portfolio&'.$term->taxonomy.'='.$term->slug);
    
    foreach($myposts as $post) : setup_postdata($post);
    
    do your loop stuff here
    
    endforeach;
  10. Morgan Kay
    Member
    Posted 2 years ago #

    Here's the print_r, when I have "genres=comics"

    stdClass Object ( [term_id] => 8 [name] => Comics [slug] => comics [term_group] => 0 [term_taxonomy_id] => 25 [taxonomy] => genre [description] =>
    
    Print comics
    [parent] => 0 [count] => 0 [filter] => )
  11. Morgan Kay
    Member
    Posted 2 years ago #

    Oh, and I meant to mention that when I say "it breaks the page," the page rendered, but the loop didn't. So I had a page with the header and the title, but where the loop should have gone, the HTML code just stopped. Here's the accompanying error message:

    PHP Warning: call_user_func(genre_update) [<a href='function.call-user-func'>function.call-user-func</a>]: First argument is expected to be a valid callback in /home/httpd/white/wp-includes/taxonomy.php on line 2442, referer: http://white.alchemycs.com/wp-admin/post-new.php?post_type=portfolio

  12. Morgan Kay
    Member
    Posted 2 years ago #

    The last bit of code you gave me didn't work: it just did the exact same thing that it has done with every single iteration of code I have tried: rendered the loop fine, but with all posts instead of just the ones in that genre.

    Argh!

  13. Morgan Kay
    Member
    Posted 2 years ago #

    I found the problem!!

    The problem was actually with how the custom taxonomy generator at http://themergency.com/generators/wordpress-custom-taxonomy/ generated the taxonomy. You can see above what I used to have. Luckily for me, the function reference for register_taxonomy actually uses "genre" as an example, so I copied and pasted that code and it just worked: http://codex.wordpress.org/Function_Reference/register_taxonomy

    And for anyone else who might be interested, here is the code that I now have on my taxonomy-genre.php, and it works like a charm:

    [Code moderated as per the Forum Rules. Please use the pastebin]

    Thank you, Security Man, for your help with this!

  14. bradvin
    Member
    Posted 2 years ago #

    Hey Morgan,

    Please let me know what the issue is with the taxonomy generator code found at http://themergency.com/generators/wordpress-custom-taxonomy/.

    I would really like to fix the issue you had so others do not get it too.

    thanks

  15. Morgan Kay
    Member
    Posted 2 years ago #

    This has been so long ago that I don't remember now what the problem was. At the time, I'm pretty sure that I left feedback on your website telling you about my problem. If you want to track it down, see if there is any difference between the code generated on your site, and the code found on this page: http://codex.wordpress.org/Function_Reference/register_taxonomy When I did this, there was a difference between the two sets of code, and the latter is what fixed my site.

Topic Closed

This topic has been closed to new replies.

About this Topic