• Resolved ubermatress

    (@ubermatress)


    I’m creating a music database site with two custom post types: tracks and albums. I’ve linked the tracks to their respective albums with a custom taxonomy for the album’s slug. On each album page, I’m trying to get a tracklist of the album by retrieving all tracks with an album_slug taxonomy that matches the slug of the current album.
    Here’s my code for that query:

    global $post;
    $post_slug=$post->post_name;
    $args = array (
    	'post_type' => 'track',
    	'tax_query' => array(
    		'taxonomy' => 'album_slug',
    		'field' => 'name',
    		'terms' => $post_slug,
    	),
    );
    $tracks_query = new WP_Query( $args );

    At the moment, it just outputs all the tracks in the database instead of limiting it to tracks with album_slug terms matching $post_slug.
    I’ve tried so many things from loads of places but any changes I make seem to make no tracks appear instead of all of them.
    I hope you can help me out.
    Cheers

Viewing 9 replies - 1 through 9 (of 9 total)
  • Hi, you need to put the 'tax_query' as an array within an array, such as this:

    $args = array(
    	'post_type' => 'post',
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'people',
    			'field'    => 'slug',
    			'terms'    => 'bob',
    		),
    	),
    );

    Taken from here:

    http://codex.wordpress.org/Class_Reference/WP_Query

    Thread Starter ubermatress

    (@ubermatress)

    Thanks a lot for your response. I’ve tried that already and I’ve left it like that now. Unfortunately that doesn’t help – now nothing is appearing at all.
    I’ve checked everything but can’t see why it’s not working. $post_slug comes out with the right value and there are definitely tracks with a matching album_slug term. Even when I manually type in a slug instead of the variable, nothing appears.

    It seems like there is some sort of mismatch between your term and what is being input into your query for taxonomy. Try something simple, like creating a term within the ‘album_slug’ taxonomy called ‘test’ and assign a post to it. Then for the field, enter ‘test’.

    Does this work?

    If it does then there definitely is a mismatch there.

    Thread Starter ubermatress

    (@ubermatress)

    I tried that but it didn’t work. I’ve checked and rechecked all the spellings and so on but I can’t for the life of me find what’s wrong. I’ve also tried other taxonomies instead but with the same results. Should I send some more code like the taxonomy registration or the loop based on that query?

    Yes. Send tax registration and the query.

    Thread Starter ubermatress

    (@ubermatress)

    Okay, here’s the taxonomy registration:

    if ( ! function_exists( 'album_slug' ) ) {
    
    		// Register Custom Taxonomy
    		function album_slug() {
    
    			$labels = array(
    				'name'                       => _x( 'Album Slug', 'Taxonomy General Name', 'text_domain' ),
    				'singular_name'              => _x( 'Album Slug', 'Taxonomy Singular Name', 'text_domain' ),
    				'menu_name'                  => __( 'Album Slug', 'text_domain' ),
    				'all_items'                  => __( 'All Album Slugs', 'text_domain' ),
    				'parent_item'                => __( 'Parent Album Slug', 'text_domain' ),
    				'parent_item_colon'          => __( 'Parent Album Slug:', 'text_domain' ),
    				'new_item_name'              => __( 'New Album Slug', 'text_domain' ),
    				'add_new_item'               => __( 'Add New Album Slug', 'text_domain' ),
    				'edit_item'                  => __( 'Edit Album Slug', 'text_domain' ),
    				'update_item'                => __( 'Update Album Slug', 'text_domain' ),
    				'separate_items_with_commas' => __( 'Separate items with commas', 'text_domain' ),
    				'search_items'               => __( 'Search Album Slugs', 'text_domain' ),
    				'add_or_remove_items'        => __( 'Add or remove album slugs', 'text_domain' ),
    				'choose_from_most_used'      => __( 'Choose from the most used album slugs', 'text_domain' ),
    				'not_found'                  => __( 'Not Found', 'text_domain' ),
    			);
    			$rewrite = array(
    				'slug'                       => 'album-slug',
    				'with_front'                 => true,
    				'hierarchical'               => false,
    			);
    			$args = array(
    				'labels'                     => $labels,
    				'hierarchical'               => false,
    				'public'                     => true,
    				'show_ui'                    => true,
    				'show_admin_column'          => true,
    				'show_in_nav_menus'          => true,
    				'show_tagcloud'              => true,
    				'rewrite'                    => $rewrite,
    			);
    			register_taxonomy( 'album_slug', array( 'dub_track' ), $args );
    
    		}
    
    		// Hook into the 'init' action
    		add_action( 'init', 'album_slug', 0 );
    
    	}

    And here’s the rest:

    <?php if ($tracks_query->have_posts()) : ?>
    	<?php while ($tracks_query->have_posts()) : $tracks_query->the_post(); ?>
    		<p><?php if (has_term('','track_no')) { $terms = get_the_terms( $post->ID , 'track_no' );  foreach ( $terms as $term ) { echo $term->name; } }; ?> - <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
    	<?php endwhile; ?>
    <?php else : ?>
    	<h4>Nothing Found</h4>
    	<p>Sorry but this album doesn&rsquo;t seem to have any tracks.</p>
    <?php endif; ?>
    <?php
    	wp_reset_postdata();
    	//END TRACK INFO
    ?>

    I’ve slightly simplified the code just to show what’s relevant. Thanks

    Hi. Sorry for the delay.

    I’m testing this on my local site and will respond.

    Hi. So I ran the test on my end, using a post instead of a custom post type.

    My guess is that for field in the tax query, you want slug instead of name. If you are inputting the slug from the post, I’m guessing that matches the slug of the album_slug terms.

    Also, you will still need to use the array within the tax query as mentioned earlier.

    Hope this helps.

    Thread Starter ubermatress

    (@ubermatress)

    Thanks for your help. I found the problem in some other code elsewhere that modified the MySQL query directly that was somehow counteracting the tax query. I’ve removed that code and done that differently and it now works as it should. I have changed the field to slug at your suggestion though.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Struggling with passing a variable to tax_query terms parameter’ is closed to new replies.