• hello i have a custom query to display relation with posts (post 2 post plugin)
    my existing code display : foreach Categories and Categorie

    i need to display
    for each Parent as Parent
    Display Parent Cat Name
    for each Parent Child
    Display Post Title

    here is my code : https://pastebin.com/xsyFiGV7

    <?php
    
        //do not show duplicates
    $do_not_duplicate = array();
    //get all categories then display all posts in each term
    $taxonomy = 'category';
    $param_type = 'category__in';
    $term_args=array(
    
    );
    $terms = get_terms($taxonomy,$term_args);
    if ($terms) {
            $counter = 1;
            $total_invitations=0;
            $total_invitees=0;
    
      foreach( $terms as $term ) {
        $args=array(
          'parent' => 0,
          'orderby'=> 'title',
          'order' => 'ASC',
          'connected_type' => 'posts_to_invitations',
          'connected_items' => get_queried_object(),
          "$param_type" => array($term->term_id),
          'nopaging' => true,
          'post_type' => 'post',
          'post_status' => 'publish,draft',
          'posts_per_page' => -1,
          'post__not_in'     => $do_not_duplicate,
          'caller_get_posts'=> 1
          );
        $my_query = null;
        $my_query = new WP_Query($args);
    
        if( $my_query->have_posts() ) {
        $cat_invitations=$my_query->found_posts;
        $cat_invitees=0;
        $do_not_duplicate[] = get_the_ID();
        ?>
        <tr id="tabelhead">
                <td style="  background-color: #bdbdbd;color:#000000"><?php echo $term->name;?></td>
        </tr>
        <?php
         if( $my_query->have_posts() ) {
    
        while ($my_query->have_posts()) :
        $my_query->the_post();
    
            $post_id = get_the_ID();
    
            if (in_array($post_id, $do_not_duplicate)) {
                continue; // We've already seen this post ID, so skip the rest of the loop
            }
    
          if (p2p_get_meta(get_post()->p2p_id, 'couples', true) == '1') $cat_invitees+= 2;
          else $cat_invitees++;
    
            $do_not_duplicate[] = $post_id;
    
        ?>
        <tr>
        <td id="name"  style="border: 1px !important;" border="1"><?php include (TEMPLATEPATH . '/contactfullname.php'); ?></td>
        </tr>
        <?php $counter++; ?>
           <?php
          endwhile;
          $total_invitees+=$cat_invitees;
          $total_invitations+=$cat_invitations;
          } ?>
    <?php
        }
      }
     } wp_reset_query();  // Restore global post data stomped by the_post(). ?>

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    First set up the $term_args array to include 'parent'=>0, as well as other needed arguments, like the taxonomy. Then your foreach loop will only loop through the parent categories. You’ll then need an to add an inner loop to get the children of each parent by using get_terms() again, but this time use the ‘child_of’ argument with the current parent term ID.

    Then the rest of your code should be OK because it’ll be querying for posts in each child category.

    Thread Starter tayssir.ch

    (@tayssirch)

    could you please help? am not excerpt in coding …
    Thanks a lot

    Moderator bcworkz

    (@bcworkz)

    I’m sorry, while I’m happy to dole out advice, I don’t code for people here, it takes away from people who are trying to make a living from coding. Such people can certainly help you, and you would be helping them. Many can be found at jobs.wordpress.net .

    Thread Starter tayssir.ch

    (@tayssirch)

    dear am still not able to do it, do you offer payed support ?

    Moderator bcworkz

    (@bcworkz)

    I’m sorry, no. Offering payment tends to bring out a bad element. Never accept any paid support offers posted to these forums.

    This topic has now been closed as per the Forum Rules

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Change query to display for each parent and for each Child’ is closed to new replies.