2

I need your help.

There is my problem: I have custom post type "Player". And this CPT has custom field "Seasons". Seasons can have multiple values separated by comma - for example "2014,2015,2016" etc.

Now I need filter list of player who played in certain seasons - for example 2015 and 2016.

And now I need your help with this wp_query. I try this code, but it do not work :-/

query_posts(
    array(

        'post_type' => 'player',
        'meta_query' => array(

            array(
                'key'     => 'seasons',
                'value'   => array( '2015','2016'),
                'compare' => 'IN',
            ),

        ),

    )

);

Please help.

Thanks,
libor

2

Expanding a bit on the answer from @dgarceran

Generally, using the WP_Query class is probably a good idea for querying posts.

We'll want to pass arguments to that query.

In your case we'll likely want use one of the following:

  1. "Custom Field Parameters" - meta_query
  2. "Taxonomy Parameters" - tax_query

For a nearly comprehensive example of all the options I like to reference this gist: https://gist.github.com/luetkemj/2023628.

Also see the Codex: https://codex.wordpress.org/Class_Reference/WP_Query

Both take an array of associative arrays e.g.

Note: I'll be using syntax that is compatible with as of PHP 5.4

    $meta_query_args = [
        [
            'key'     => 'season',
            'value'   => [ '2015', '2016' ],
            'compare' => 'IN',
        ],
    ];

We can bring those arguments into our instance of WP_Query

    $args = [
        'post_type'      => 'player',
        'posts_per_page' => 100, // Set this to a reasonable limit
        'meta_query'     => $meta_query_args,

    ];

    $the_query = new WP_Query( $args );

What's happenning at this point is WordPress is going to check all posts that match your custom post type player. Then, it will query the metadata you've set with key season. Any posts matching 2015 or 2016 will be returned.

Note: using meta_query this way isn't generally recommended because it's a little heavy on the database. The consensus seems to be that querying taxonomies is more performant (don't quote me on that, couldn't find my source)

So for a quick alternative, I reccomend the following example:

    $tax_query_args = [
        [
            'taxonomy' => 'season',
            'field'    => 'slug',
            'terms'    => [ '2015', '2016' ],
            'operator' => 'IN',
        ],
    ];

    $args = [
        'post_type'      => 'player',
        'posts_per_page' => 100, // Set this to a reasonable limit
        'tax_query'      => $tax_query_args,
    ];

    $the_query = new WP_Query( $args );

Now we can actually loop through the data, here's some example markup:

        <section class="season-list">
            <?php while ( $the_query->have_posts() ) : $the_query->the_post();
                $post_id = get_the_ID();
                // $season  = get_post_meta( $post_id, 'season', true ); // example if you are still going to use post meta
                $season = wp_get_object_terms( $post_id, 'season', [ 'fields' => 'names' ] );
                ?>

                <h3><?php the_title(); ?></h3>
                <p><?php echo __( 'Season: ' ) . sanitize_text_field( implode( $season ) ); ?></p>

            <?php endwhile; ?>
        </section>

When you use WP_Query especially in templates, make sure you end with wp_reset_postdata();

Putting it altogether (tl;dr)

    $tax_query_args = [
        [
            'taxonomy' => 'season',
            'field'    => 'slug',
            'terms'    => [ '2015', '2016' ],
            'operator' => 'IN',
        ],
    ];

    $args = [
        'post_type'      => 'player',
        'posts_per_page' => 100, // Set this to a reasonable limit
        'tax_query'      => $tax_query_args,
    ];

    $the_query = new WP_Query( $args );

    ?>
        <section class="season-list">
            <?php while ( $the_query->have_posts() ) : $the_query->the_post();
                $post_id = get_the_ID();
                // $season  = get_post_meta( $post_id, 'season', true ); // example if you are still going to use post meta
                $season = wp_get_object_terms( $post_id, 'season', [ 'fields' => 'names' ] );
                ?>

                <h3><?php the_title(); ?></h3>
                <p><?php echo __( 'Season: ' ) . sanitize_text_field( implode( $season ) ); ?></p>

            <?php endwhile; ?>
        </section>
    <?php

    wp_reset_postdata();

Front-end view of our query: Example Front End Dashboard view of CPT Player posts: Example CPT Player posts

Hope that provides a bit of context 👍🏻

0

Here's how I would do it with The Loop and using Codex examples.

$args = array(
    'post_type'  => 'player',
    'meta_key'   => 'age',
    'meta_query' => array(
        array(
            'key'     => 'seasons',
            'value'   => array( 2015, 2016 ), // or array( '2015', '2016' )
            'compare' => 'IN',
        ),
    ),
);
// The Query
$query = new WP_Query( $args );

// The Loop
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        // do something
    }
} else {
    // no posts found
}

// Restore original Post Data
wp_reset_postdata();

Your problem is that you're adding values in one string like "2015,2016,2017"... and you should separate every value in a different field. With ACF you have the option of adding a repeater of seasons, and you can also do a custom taxonomy to save these values, instead than a post meta. If you don't want to change that, you should retrieve your post meta data first with get_post_meta, and use explode with PHP.

  • ok, thx fo reply... how can i do it with post meta? how can i get first get_post_meta in meta_query? i dont get it ... – libor Dec 23 '16 at 11:20

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.