• With the following code I give out all postings of my pd “release” with the taxonomy “kategorie”.

    But how can I get it now, if I wanna do the same (example on an single page) from the parent term (of taxonomy) ?

    <?php 
    $terms_array = array( 
      'taxonomy' => 'kategorie', 
      'orderby' => 'ID',
      'order' => 'ASC',
    );
    $services_terms = get_terms($terms_array); 
    foreach($services_terms as $service): ?>
    
    <div id="content_releases">
    <div class="kategorie">
    <span>
      
    <?php	if (get_locale() == 'en_US') {
       echo $service->name;
       echo '<span>';
       echo '<a href="/releases/';
       echo $service->slug;
       echo '"';
       echo ' target="_parent">';
       echo 'view all';
       echo ' ';
       echo $service->name;
       echo '</a>';
       echo '</span>';
    }?>
    
    <?php	if (get_locale() == 'de_DE') {
       echo $service->name;
       echo '<span>';
       echo '<a href="/de/releases/';
       echo $service->slug;
       echo '"';
       echo ' target="_parent">';
       echo 'alle';
       echo ' ';
       echo $service->name;
       echo ' ';
       echo 'ansehen';
       echo '</a>';
       echo '</span>';
    }?>
    
    <?php	if (get_locale() == 'es_ES') {
       echo $service->name;
       echo '<span>';
       echo '<a href="/es/releases/';
       echo $service->slug;
       echo '"';
       echo ' target="_parent">';
       echo 'ver todos';
       echo ' ';
       echo $service->name;
       echo '</a>';
       echo '</span>';
    }?>
    
    </span>
    <div class="clear"></div>
    </div>
    
    <?php 
    $post_args = array(
          'posts_per_page' => '5',
          'post_type' => 'release', 
          'tax_query' => array(
              array(
                  'taxonomy' => 'kategorie',
                  'field' => 'term_id', 
                  'terms' => $service->term_id,
                  
              )
          )
    );
    $myposts = get_posts($post_args); ?>
    
    <?php foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
    <div class="release_wrap">
    <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>
    <h2><a href="<?php the_permalink(); ?>"><?php echo pods_field_display( 'voe_titel' );?></a>
    von <?php echo pods_field_display( 'artist' );?> <?php echo pods_field_display( 'featuring_angabe' );?> <?php echo pods_field_display( 'featuring_artist' );?></h2>
    
    </div>
    
    <?php endforeach; // Term Post foreach ?>
    
    <div class="clear"></div>
    <?php wp_reset_postdata(); ?>
    
    <?php endforeach; // End Term foreach; ?>  
    
    
Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    Hello again ??

    You want to get posts assigned the parent of the current $service term instead of the $service term itself? Instead of $service->term_id use $service->parent. But if $service->parent is 0, the current term is the top level term and has no parent. You could use the ternary operator (if/then/else shorthand) to use the parent ID if there is one, otherwise use the current term ID.
    'terms' => 0 == $service->parent ? $service->term_id : $service->parent,

    Thread Starter mallorcagirl76

    (@mallorcagirl76)

    No sorry I wanna have the postings from the taxonomie “Kategorie” in pod “release” with the term of the current posting.

    Thread Starter mallorcagirl76

    (@mallorcagirl76)

    The term under “Kategorie”

    Moderator bcworkz

    (@bcworkz)

    Oh, so you want to get other posts assigned the same terms as the current post? Use wp_get_post_terms() to get the current post’s terms. Specify ['fields' => 'ids',] as its $args parameter, as you only need the term IDs, not entire objects. Use the returned IDs in your WP_Query args as the 'terms' parameter.

    You’ll likely also want to add a 'post__not_in' arg to WP_Query, passing the current post’s ID. This prevents the current post from being part of the query results.

    Thread Starter mallorcagirl76

    (@mallorcagirl76)

    I need it not in template. I need it hardcoded as php

    Moderator bcworkz

    (@bcworkz)

    You’d then need a hook that fires on the page where you want output to appear. Preferably one inside the loop, but not essential if you’re targeting single pages. “the_content” filter is commonly used, but where the added output occurs on the page is not always optimal. You get the current post ID from the global $post object, or use get_the_ID(). In the case of a single post or page, you could also use get_queried_object_id()

    With filter hooks, instead of echoing content like we do on a template, we accumulate output into a variable and return the final result. With template action hooks you may directly echo out content.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Get postings from the parent post term’ is closed to new replies.