logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$40
Display custom post types within a specific category

Plugins being used: Advanced Custom Fields

I have a custom post type named Tournaments. I assign categories based on the status of the Tournament: upcoming-tournament or finished-tournament.

I have a page made for each team in the Volleyball club, i.e. Team Blue. I created a Relationship Custom Field named "tournaments" that is available to each team page.

I'd like to display the 4 most recent Tournaments that are categorized as upcoming-tournament. I'd then like to display the 4 most recent Tournaments that are categorized as finished-tournament.

I've included the start of my code below. What I need help with is filling in the areas that are commented out. Basically, I need help writing the loop!


<!-- this will grab all the tournaments that the team is participating in -->
<?php $tournaments = get_field( 'tournament_selection' );

if( $tournaments ) { ?>

<h3>Upcoming Tournaments</h3>

<?php foreach( $tournaments as $tournament ) {

// if $tournament has taxonomy "upcoming-tournament"...

} ?>



<h3>Finished Tournaments</h3>

<?php foreach( $tournaments as $tournament ) {

// if $tournament has taxonomy "finished-tournament"...

} ?>

<?php } ?>

This question has been answered.

Dave Schmidt | 01/25/13 at 4:42pm Edit
Tutorial: How to assign prize money

Previous versions of this question: 01/25/13 at 8:34pm

(31) Responses

See a threaded view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    01/25/13
    4:47pm
    Francisco Javier Carazo Gil says:

    You can use has_term: http://codex.wordpress.org/Function_Reference/has_term

  • avatar
    Last edited:
    01/25/13
    4:50pm
    Dylan Kuhn says:

    I think even without loading the post object, this may work:


    if ( has_term( 'your-taxonomy', 'finished-tournament', $tournament ) ) {
    // do something
    }

  • avatar
    Last edited:
    01/25/13
    4:54pm
    John Cotton says:

    I think you probably need to use wp_get_object_terms.

    I assuming that your $tournaments is an array of custom post ids? If so then


    <?php
    if( $tournaments ) {
    $upcoming = '';
    $finished = '';

    foreach( $tournaments as $tournament ) {
    // You could get more info back by changing the fields param, but that would make the in_array compare different...
    $terms = wp_get_object_terms( $tournament, array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'slug') );

    if( in_array( 'upcoming-tournament', $terms ) ) {
    $upcoming .= 'Upcoming!' // edit to whatever it is you want to say
    }

    if( in_array( 'finished-tournament', $terms ) ) {
    $upcoming .= 'Finished!' // edit to whatever it is you want to say
    }
    }
    ?>

    <h3>Upcoming Tournaments</h3>
    <?php echo $upcoming; ?>

    <h3>Finished Tournaments</h3>
    <?php echo $finished; ?>

  • avatar
    Last edited:
    01/25/13
    4:48pm
    Francisco Javier Carazo Gil says:

    Make the loop and change the foreach for the loop.

    Then, with the post object loaded, you can do directly it: if( has_term( 'your-taxonomy', 'finished-tournament' ) ) {
    // do something
    }

  • avatar
    Last edited:
    01/25/13
    5:19pm
    Dave Schmidt says:

    Hi John,

    I'm getting an error on the page. http://vitalvolleyball.com/teams/test/

    Your syntax looks fine though. Any ideas? Here's the code I'm adding:


    <?php

    $tournaments = get_field( 'tournament_selection' );

    if( $tournaments ) {

    $upcoming = '';

    $finished = '';

    foreach( $tournaments as $tournament ) {

    // You could get more info back by changing the fields param, but that would make the in_array compare different...

    $terms = wp_get_object_terms( $tournament, array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'slug') );



    if( in_array( 'upcoming-tournament', $terms ) ) {

    $upcoming .= 'Upcoming!' // edit to whatever it is you want to say

    }



    if( in_array( 'finished-tournament', $terms ) ) {

    $finished .= 'Finished!' // edit to whatever it is you want to say

    }

    }

    ?>



    <h3>Upcoming Tournaments</h3>

    <?php echo $upcoming; ?>



    <h3>Finished Tournaments</h3>

    <?php echo $finished; ?>



    Any thoughts?

  • avatar
    Last edited:
    01/25/13
    5:21pm
    John Cotton says:

    It's missing a } before the last ?><h3>Upcoming Tournaments</h3>

  • avatar
    Last edited:
    01/25/13
    5:25pm
    John Cotton says:

    Actually, it should be like this in case there are no tournaments:




    <?php



    $tournaments = get_field( 'tournament_selection' );



    if( $tournaments ) {



    $upcoming = '';



    $finished = '';



    foreach( $tournaments as $tournament ) {



    // You could get more info back by changing the fields param, but that would make the in_array compare different...



    $terms = wp_get_object_terms( $tournament, array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'slug') );







    if( in_array( 'upcoming-tournament', $terms ) ) {



    $upcoming .= 'Upcoming!' // edit to whatever it is you want to say



    }







    if( in_array( 'finished-tournament', $terms ) ) {



    $finished .= 'Finished!' // edit to whatever it is you want to say



    }



    }



    ?>

    <h3>Upcoming Tournaments</h3>
    <?php echo $upcoming; ?>

    <h3>Finished Tournaments</h3>
    <?php echo $finished; ?>
    <?php
    }
    ?>

  • avatar
    Last edited:
    01/25/13
    5:29pm
    Dave Schmidt says:

    Hmm, still getting a page error. I deleted everything else in the template and still get the error. Your idea looks promising though!

  • avatar
    Last edited:
    01/25/13
    5:30pm
    Dave Schmidt says:

    Any ideas on how to put that all together? Consider me a novice multiple loops kind of guy.

  • avatar
    Last edited:
    01/25/13
    5:32pm
    John Cotton says:

    Have you copied my last code?

    Can you pastebin your template so that I can see the whole context?

    You're just missing a closing bracket somewhere I think

  • avatar
    Last edited:
    01/25/13
    5:38pm
  • avatar
    Last edited:
    01/25/13
    5:42pm
    John Cotton says:

    These two lines are missing semi-colons:


                            
    $upcoming .= 'Upcoming!'


    $finished .= 'Finished!'

  • avatar
    Last edited:
    01/25/13
    5:45pm
    Dave Schmidt says:

    Believe it or not they're actually in there. I selected PHP as the syntax on Pastebin and it stripped them out. But in my WP template they're there. Hmmm...

  • avatar
    Last edited:
    01/25/13
    5:46pm
    Dave Schmidt says:

    Hang on, I see what you were referring to. Those two calls were missing the semi-colons at the top of the query. Sorry!

  • avatar
    Last edited:
    01/25/13
    5:50pm
    Dave Schmidt says:

    John, looks like we're getting closer. The page is spitting out some array() errors. Check it out: http://vitalvolleyball.com/teams/test/

  • avatar
    Last edited:
    01/25/13
    5:51pm
    John Cotton says:

    Soory, also, this line needs editing to add the taxonomies:


    $terms = wp_get_object_terms( $tournament, array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'slug') );


    should be


    $terms = wp_get_object_terms( $tournament, 'category', array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'slug') );

    or whatever taxonomy you are using (you can pass an array if it's more than one)

  • avatar
    Last edited:
    01/25/13
    5:55pm
    Dave Schmidt says:

    OK, so I don't have a custom taxonomy set up for the custom post type. I'm just using global categories. Any ideas how I set that up in the 'category' section of the $terms?

  • avatar
    Last edited:
    01/25/13
    5:56pm
    John Cotton says:

    Any ideas how I set that up in the 'category' section of the $terms?


    That's what my code does. Just copy the corrected line in place of the incorrect one.

  • avatar
    Last edited:
    01/25/13
    6:00pm
    Dave Schmidt says:

    So if I'm passing in the possible two categories, is this how the array should look like? Because it's giving me an error. I'm really terrible at this stuff, sorry John!


    $terms = wp_get_object_terms( $tournament, array('upcoming-tournament', 'finished-tournament'), array('orderby' => 'meta_value', 'meta_key' => 'tournament_date', 'order' => 'ASC', 'fields' => 'slug') );

  • avatar
    Last edited:
    01/25/13
    6:05pm
    John Cotton says:

    The line is just

    $terms = wp_get_object_terms( $tournament, 'category', array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'slug') );


    The middle param is the taxonomy NOT the terms.

  • avatar
    Last edited:
    01/25/13
    6:11pm
    Dave Schmidt says:

    OK cool. By the way, I'm going to increase the payout on this John! So stick with me until it's sorted out. :)

    The errors disappeared. That's good!

    So here's what I want to display for any posts that match the criteria:


    <ul>
    <li>
    <a href="<?php echo get_permalink(); ?>"><?php the_field('tournament_name', $tournament->ID); ?></a>

    <span>
    <?php $date = DateTime::createFromFormat('Ymd', get_field('tournament_date', $tournament->ID));
    echo $date->format('F d, Y'); ?>
    </span>
    </li>
    </ul>

  • avatar
    Last edited:
    01/25/13
    6:23pm
    John Cotton says:

    So here's what I want to display for any posts that match the criteria:


    Stick this somewhere near the top (having a function makes the code neater and saves repeating...)

    function format_tournament( $id ) {
    $date = DateTime::createFromFormat( 'Ymd', get_field('tournament_date', $id) );

    return sprintf( '<li><a href="%s">%s</a><span>%s</span></li>', get_permalink($id), get_field('tournament_name', $id), $date->format('F d, Y') );

    }



    And mod the loop as follows:


    foreach( $tournaments as $tournament ) {

    $terms = wp_get_object_terms( $tournament, array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'slug') );

    if( in_array( 'upcoming-tournament', $terms ) ) {
    $upcoming .= format_tournament( $tournament->ID );
    }


    if( in_array( 'finished-tournament', $terms ) ) {
    $finished .= format_tournament( $tournament->ID );
    }
    }


    Finally, change the output to


    <h3>Upcoming Tournaments</h3>
    <ul><?php echo $upcoming; ?></ul>


    <h3>Finished Tournaments</h3>
    <ul><?php echo $finished; ?></ul>

  • avatar
    Last edited:
    01/25/13
    6:43pm
    Dave Schmidt says:

    OK, code looks great! Unfortunately nothing is displaying on the page. I verified that there are Tournaments categorized as both "upcoming-tournament" and "finished-tournament." Here is the code I have in the template. Sorry to be a pain, but I feel we're so close!


    <?php

    $tournaments = get_field( 'tournament_selection' );


    function format_tournament( $id ) {
    $date = DateTime::createFromFormat( 'Ymd', get_field('tournament_date', $id) );
    return sprintf( '<li><a href="%s">%s</a><span>%s</span></li>', get_permalink($id), get_field('tournament_name', $id), $date->format('F d, Y') );
    }


    if( $tournaments ) {

    $upcoming = '';
    $finished = '';


    foreach( $tournaments as $tournament ) {

    $terms = wp_get_object_terms( $tournament, 'category', array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'slug') );

    if( in_array( 'upcoming-tournament', $terms ) ) {
    $upcoming .= format_tournament( $tournament->ID );
    }

    if( in_array( 'finished-tournament', $terms ) ) {
    $finished .= format_tournament( $tournament->ID );
    }
    }

    ?>


    <h3>Upcoming Tournaments</h3>
    <ul><?php echo $upcoming; ?></ul>

    <h3>Finished Tournaments</h3>
    <ul><?php echo $finished; ?></ul>


    <?php

    }

    ?>

  • avatar
    Last edited:
    01/25/13
    6:51pm
    John Cotton says:

    This

    format_tournament( $tournament->ID );


    should be

    format_tournament( $tournament );

  • avatar
    Last edited:
    01/25/13
    7:06pm
    Dave Schmidt says:

    I still don't get anything to display. :(

    Here's a pastebin: http://pastebin.com/ivuprSrr

    And if you take a look at the test page I did a variable dump. So I can tell that the tournaments are being pulled in correctly.

  • avatar
    Last edited:
    01/25/13
    7:16pm
    John Cotton says:

    OK my mistake - try this:


    foreach( $tournaments as $tournament ) {
    print_r( $tournament->ID );
    $terms = wp_get_object_terms( $tournament->ID, 'category', array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'slug') );

    if( in_array( 'upcoming-tournament', $terms ) ) {
    $upcoming .= format_tournament( $tournament->ID );
    }

    if( in_array( 'finished-tournament', $terms ) ) {
    $finished .= format_tournament( $tournament->ID );
    }
    }


    If that works, then the print_r line can be removed. If not, it might tell us something.

  • avatar
    Last edited:
    01/25/13
    7:30pm
    Dave Schmidt says:

    So nothing displays, but the print_r does display the 3 ID's of the tournaments that are supposed to be visible. So at least it can pull in the ID's.

  • avatar
    Last edited:
    01/25/13
    7:36pm
    John Cotton says:

    So just so we know completely what's going on try

    print_r( $tournament->ID ); echo '<br/>';
    $terms = wp_get_object_terms( $tournament->ID, 'category', array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'slug') );
    print_r( $terms ); echo '<br/>';

  • avatar
    Last edited:
    01/25/13
    7:37pm
    Dave Schmidt says:

    John, you have an honest face. Would it help if I messaged you some FTP creds?

  • avatar
    Last edited:
    01/25/13
    7:38pm
    Dave Schmidt says:

    Done. Nothing is showing up in the array.

  • avatar
    Last edited:
    01/25/13
    7:56pm
    John Cotton says:

    John, you have an honest face. Would it help if I messaged you some FTP creds?

    :) It would probably be quicker...

This question has expired.



Gabriel Reguly, Christianto, Dave Schmidt, Arnav Joy voted on this question.



Current status of this question: Completed



Please log in to add additional discourse to this page.





Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.