• Hi Everyone,
    I am working on a record label site for a friend and have a catalog section with all the releases in it. Each release is tagged by its respective artist (sometimes more than one artist). Now I am creating the artist section and want to pull in the respective release(s) for each artist into their page. So far I have been using WP_Query for a second loop and it has been working well:

    <?php $my_query = new WP_Query(‘category_name=catalog&tag=my tag’); ?>

    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    //my content goes//
    <?php endwhile; ?>

    However I want the tag= to be a custom field key (depending on the artist name). I know how to add custom fields to a post, but dont know if what I want to do is possible. Can anyone help?

    Thanks for your time.

Viewing 14 replies - 1 through 14 (of 14 total)
  • <?php
    $cat_id = get_cat_ID('catalog');
    $args=array(
      'cat' => $cat_id,
      'meta_key' => 'author'
      'meta_value' => 'author_value_here',
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'List of Posts';
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
    Thread Starter moodyjive

    (@moodyjive)

    Michael – Thanks for this I will try tonight!
    Please forgive my ignorance but I don’t see any reference to “tag” in this code, how will it pick up the release posts that are tagged by artist? From what I see i will make custom fields in the array look like this:

    ‘meta_key’ => ‘artists_name’
    ‘meta_value’ => ”,

    But how does it pick up the tag?

    Thanks!

    Yeah, because it really didn’t make sense–you said “I want the tag= to be a custom field key”. You want to query posts with a tag derived from a custom field, but the custom field is attached to a post.

    Thread Starter moodyjive

    (@moodyjive)

    Maybe I have been unclear. Catalog is one category, Artist is another. In Catalog I have posts like “I Wanna Rock” by Artist Zero (the artist name is a custom field BTW in Catalog). This post is tagged Artist Zero. Now I want to create an artist post (in the Artist category) called “Artist Zero” and pull in any Catalog posts tagged “Artist Zero”. In order to do this I thought i would query posts from the Catalog category that are tagged “Artist Zero”. I figured I could add a custom field in the “Artist Zero” post that would tell it to pull in those tagged items.

    Maybe I am going about this the wrong way? Should I try to query posts by the artist custom field in the Catalog section?

    (the artist name is a custom field BTW in Catalog)

    How are you attaching a custom field to a category? Custom fields are attached to posts, not categories.

    Thread Starter moodyjive

    (@moodyjive)

    Yes I am using a custom field in each Catalog post named artist_name (key) and the value changes per post

    Thread Starter moodyjive

    (@moodyjive)

    Sorry I wasn’t clearer

    Should I try to query posts by the artist custom field in the Catalog section?

    Sounds like the way to go.

    Thread Starter moodyjive

    (@moodyjive)

    One more question πŸ˜‰
    I used your code as follows:

    <?php
    $cat_id = get_cat_ID('catalog');
    $args=array(
      'cat' => $cat_id,
      'meta_key' => 'artist_name',
      'meta_value' => '',
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'List of Posts';
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
    //content
    
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    Unfortunately it is returning every post that contain the custom field “artist_name”
    as opposed to the certain value I have entered. Is this possible?

    Change that line 'meta_value' => '',

    to something like

    'meta_value' => 'specific value you want here',
    Thread Starter moodyjive

    (@moodyjive)

    Thanks for your help, but I am trying to pass the value from the post as the value will change on each post. Sometimes the meta_value will be “artist1” and other times it will be “artist2”

    That is why I tried:
    'meta_value' => '',

    I tried:
    'meta_value' => '$value',
    That doesn’t work.

    Is there any way to do this?

    Not sure when the value would be different but:

    $value = 'value1';
    .
    .
    .
    'meta_value' => $value,
    Thread Starter moodyjive

    (@moodyjive)

    OK that didn’t work but I was able to solve it with get_post_custom_values:

    <?php
    $cat_id = get_cat_ID('catalog');
    $artist_name_values = get_post_custom_values('artist_name');
    $args=array(
      'cat' => $cat_id,
      'meta_key' => 'artist_name',
      'meta_value' => $artist_name_values,
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
    //my content
    
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    Now I want to have multiple values show up for the meta_key “artist_name”. Is there any way to do this?

    Now I want to have multiple values show up for the meta_key “artist_name”. Is there any way to do this?

    You’d have to resort to using wpdb for something like that.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Query posts by tag and custom field’ is closed to new replies.