1

Urgently need help. Please direct me to the proper forum if necessary, but I very much need to solve this.

I'm creating a property management site in which the basic functionality must work like this:

  • User logs on and sees buildings/properties under their management.
  • User clicks a building and sees categories of service/repair items (electrical, sewer, internet, etc.).
  • User selects a category and sees unique service items in that category for that particular building (where the cable box is, where the breaker box is, etc.).

My issue is that I'm getting the service items for all buildings currently, not just the one being managed. The current flow goes from a page named Buildings which shows all available building posts (which I've customized with a file template-buildings.php) to the single page for the custom post type 'buildings' which displays a list of the categories (handled by single-buildings.php) to the category.php page to display all the service items in selected category to the single.php page where an individual item is displayed.

My big breakdown seems to be on category.php. I'm trying to display ONLY the items in a category that have to do with the previously selected building post. I'm trying to use the posts-to-posts plugin to enable that. Here's the code so far.

 <?php 
        // Get category id of current page
        $catid = get_queried_object_id();

        // Find connected posts
        $wp_query = new WP_Query( array(
          'post_type' => 'buildings'
        ) );
        p2p_type( 'buildings_to_si' )->each_connected( $wp_query, array( 'cat' => $catid ), 'service_items' );

        // Display connected posts
        while ( $wp_query->have_posts() ) : $wp_query->the_post();

        foreach ( $post->service_items as $post ) : setup_postdata( $post );

      ?>

      <div class="col-lg-3 col-md-4 col-sm-6 p-3 text-center">
        <a class="btn btn-elegant" href="<?php the_permalink(); ?>">
          <?php the_title(); ?>
        </a>
      </div>

      <?php endforeach; ?>

      <?php endwhile; ?>

As of now, I get all of the posts in a category for all of the buildings, regardless of the relationship set by the plugin. I know it's got to do with the fact that I'm trying to do this on the category archive page, but I really don't know what else to do at this point. I'm willing to go a totally different route if necessary. Will update for clarification if needed. Thanks!

Edit: For Posterity

So I got it to work based on the comment from kuchenundkakao. I added this code to the single-buildings.php file that adds the post id to the category link address (updated code is in bold)

<?php

      $args = array(
        'hide_empty' => true,
        'orderby' => 'name',
        'order' => 'ASC', 
        'parent' => 0,
        'posts_per_page' => -1
      );
      $categories = get_categories($args);

      foreach($categories as $category) { 

        // Get the ID of a given category
        $category_id = get_cat_ID( $category->name );
        // Get the URL of this category
        $category_link = get_category_link( $category_id );

        /////////// Added bit of code /////////////////
        $category_link .= "?building_id=".get_the_ID();

        echo '<div class="col-lg-3 col-md-4 col-sm-6 p-3 text-center"><a class="btn btn-lg btn-elegant" href="' . $category_link.'" role="button">' .      
        $category->name.'</a></div>';


    ?>

Then on category.php I added a couple variables to retrieve that id. Here is the updated code based on that.

<?php 
        // Get category id of current page
        $catid = get_queried_object_id();

        ////////// Added code ////////////////
        $buildingid = $_GET['building_id'];

        // Find connected posts
        $wp_query = new WP_Query( array(
          'post_type' => 'buildings',

        ////////// Added code ////////////////
          'p' => $buildingid
        ) );
        p2p_type( 'buildings_to_si' )->each_connected( $wp_query, array( 'cat' => $catid ), 'service_items' );

        // Display connected posts
        while ( $wp_query->have_posts() ) : $wp_query->the_post();

        foreach ( $post->service_items as $post ) : setup_postdata( $post );

      ?>

      <div class="col-lg-3 col-md-4 col-sm-6 p-3 text-center">
        <a class="btn btn-elegant" href="<?php the_permalink(); ?>">
          <?php the_title(); ?>
        </a>
      </div>

      <?php endforeach; ?>

      <?php endwhile; ?>

This has fixed the issue and allowed me to keep using the category page as intended. It does add a bit of mess to my urls, but this is a private site for a company, so that won't be an issue.

0
1

I'm not that familiar with the posts-to-posts Plugin, but your main problem may be that the category page doesn't know which building you chose in the step before if you don't transmit it in a parameter or save it in a session or cookie. Maybe it would be better to output the connected items on the buildings page, like this:

 Building (description etc)
 - category 1
       -item 1
 - category 2
         -item 2
         -item 3
         ...
4
  • Yeah I figured that was my problem. I was hoping there was a way to carry the building id or something to the category page. It is starting to look like I just need to move it to the building page.
    – KreigD
    Sep 8 '17 at 8:54
  • 1
    Well, you can carry it over. It just doesn't look as good in the address bar: add something like "?building_id=".get_the_ID() to the category links. Then in the Category code, you get the ID by $_GET['building_id']. Sep 8 '17 at 9:01
  • I'll try that out tomorrow morning and see if it's worth doing that way or not. I'll update in the morning. Thanks!
    – KreigD
    Sep 8 '17 at 9:08
  • I used that code you suggested to solve the issue. Updated my post. Thanks!
    – KreigD
    Sep 8 '17 at 21:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.