Home Forums Front-end Issues Order relationship query by custom field


Order relationship query by custom field

  • I have an event website with 3 CPT, Speakers, Activities and Partners, all of them with relationship fields.

    Speakers are employees from Partners and might play some Activities. There’re also two custom fields which set the date and the time of these activities.

    On the page I show every person, I need to show the activities they’ll play on the event next to the speaker. I’ve tried some things to make things happen, and following the last example from this article, I can get things working but a really weird bug is happening: some activities show duplicated and with a different value on the time custom field!

    I’m really losing my mind here, tried a lot of different approaches but I think I’m missing something here.

    I’m also using Post2Post plugin to make content managing easier for the client.

    Here are my snippets (some words in portuguese because so):

    get_posts from related activities

    $atv = get_posts(array(
    	'post_type' => 'atividade', // activity
    	'meta_query' => array(
    			'key' => 'rel_participantes', // relationship field
    			'value' => '"'.get_the_ID().'"',
    			'compare' => 'LIKE'
    	'meta_key' => 'hora', // hour
    	'orderby' => 'meta_value',
    	'order' => 'ASC'

    Trying to print the activities inside a people CPT loop:

    if($atv) {
    	foreach($atv as $post) {
    		$data = date("d/m", strtotime(get_field('data', $post->ID))); // date
    		$hora = get_field('hora', $post->ID); // time
    		$ano = wp_get_post_terms($post->ID, 'ano')[0]->slug; // ano = year custom taxonomy
    		if($ano == $classe->slug) {
    			echo "<h6><time>".$data." - ".$hora."</time></h6>";
    			echo "<p>".get_the_title()."</p>";

    Some light over here? 🙂

Viewing 1 post (of 1 total)

The topic ‘Order relationship query by custom field’ is closed to new replies.