• Resolved Corinne

    (@cityheadache)


    Hi, I’ve spent hours trying to figure out the problem with my query, but have narrowed it down to “connected_meta” and “connected_orderby” not playing well together.

    I am building a discography, powered by two post types: ‘release’ and ‘track. I have created a reciprocal connection between the two post types, and I’ve created two fields: ‘order’ and ‘disc’ for the connections.

    On my release template, I have this query to select the discs:

    <?php $tracks = get_posts(
    	array(
    		'post_type' => 'track',
    		'connected' => $post->ID,
    		'numberposts' => -1,
    		'nopaging' => true,
    		'suppress_filters' => false,
    		'connected_orderby' => 'disc',
    		'connected_order' => 'asc'
    	)
    ); ?>
    <?php foreach ($tracks as $each) { ?>
    	<?php $discs[] = p2p_get_meta( $each->p2p_id, 'disc', true ); ?>
    <?php } ?>
    <?php $nodupes = array_unique($discs); ?>

    This works fine, and alphabeticises and hides all duplicate disc names, so it echoes “CD1, CD2” etc.

    Then I have this to select all tracks on each disc (ie. have the meta value of the disc name for the field “disc”) within a foreach loop of each disc:

    <?php $unique = get_posts(
    	array(
    		'post_type' => 'track',
    		'connected' => $post->ID,
    		'numberposts' => -1,
    		'nopaging' => true,
    		'suppress_filters' => false,
    		'connected_meta' => array( 'disc' => $disc ),
    		'connected_orderby' => 'order',
    		'connected_order' => 'asc'
    	)
    ); ?>

    This displays nothing under each disc. But if I take out the ‘connected_orderby’ part of the query, it displays the correct related tracks but in an unsorted order. On the other hand, if I keep the order part of the query and take out the ‘connected_meta’, I get all tracks in the database ordered by the ‘order’ meta value.

    I’ve found it impossible to use both ‘connected_orderby’ and ‘connected_meta’ in conjunction with each other. Is there something I’m doing wrong with my query? At the moment I’m having to use <?php if ($disc == p2p_get_meta( $track->p2p_id, 'disc', true )) { ?> within my loop to retrieve the correct tracks for the disc, which obviously isn’t the best way to do it.

    Any help?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author scribu

    (@scribu)

    Hello,

    It was a bug in the generated SQL query, when both ‘connected_meta’ and ‘connected_orderby’ are used.

    It should work now, if you download the development version (0.8.1-alpha2).

    Thanks for reporting!

    Thread Starter Corinne

    (@cityheadache)

    Thanks for the quick reply! I will download the dev version now. This is an incredibly useful plugin!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Query problem: connected_meta & connected_orderby’ is closed to new replies.