• Resolved traveler

    (@visualeight)


    I have 3 custom post types, “closet”, “outfit”, and “packing list”. A client has his/her own single “closet” post, but clients can have multiple “outfit” posts and multiple “packing list” posts.

    The name of the client’s “closet” post is the client’s firstName + lastName, so ‘Tom Jones’. There is also a custom “client” taxonomy for each client using the same firstName + lastName convention as the title of the client’s “closet” post.

    The point of this is so when viewing a client’s “closet” post, I can also see how many “outfits” or “packing lists” have been created for that client from a custom meta box showing post title and permalink to “edit” screen for that post. I also have each meta box generating a link to create a new “outfit” or “packing list” directly from within the client’s closet.

    I have this meta box code:

    
    <?php
    // ADDS META BOX TO 'CLOSET' SCREEN SHOWING OUTFITS CREATED FOR THIS CLIENT
    
    // ADDS FONT AWESOME TO 'CLOSET' ADMIN SCREEN
    function queue_scripts_to_closet() {
       wp_enqueue_style( 'le-font-awesome-css', 'https://pro.fontawesome.com/releases/v5.10.0/css/all.css' );
    }
    add_action('admin_enqueue_scripts', 'queue_scripts_to_closet');
    
    // CREATE THE META BOX
    function le_outfit_loop_meta_box() {
        $screens = ['closet'];
        foreach ($screens as $screen) {
            add_meta_box(
                'outfit-loop-meta-wrapper',      // Unique ID
                'Outfit History',                // Box title
                'le_outfit_loop_html',           // Content callback function, must be of type callable
                $screen,
                'side'
            );
        }
    }
    add_action('add_meta_boxes', 'le_outfit_loop_meta_box');
    
    // HTML TO RENDER THE META BOX
    function le_outfit_loop_html($post) {
       // Create nonce,
       // not sure if needed since this is not saving values.
       wp_nonce_field(plugin_basename(__FILE__), 'le-custom-nonce');
       
       global $post;
       $editScreenId = $post->ID;
       $clientTaxTerm = $post->post_title;
       // echo $clientTaxTerm;
       ?>
       
       <div class="admin-button-wrapper">
          <a href="/wp-admin/post-new.php?post_type=outfit" 
             id="js-create-outfit-btn" 
             class="btn">
             <i class="fas fa-plus-circle">&nbsp;</i>
             Create New Outfit
          </a>
       </div>
    
       <?php
       $outfitLoopArgs = array( 
          'post_type' => 'outfit',
          'tax_query' => array(
             array(
                'taxonomy'  => 'client',
                'field'     => 'slug',
                'terms'     => $clientTaxTerm,
             ),
          ),
       ); 
    
       $outfitLoop = new WP_Query( $outfitLoopArgs ); 
    	if ($outfitLoop->have_posts()) : ?>
          <ol>
          <?php
             while ( $outfitLoop->have_posts() ) : 
                $outfitLoop->the_post(); 
                ?>
                <li>
                   <a target="_blank" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                </li>
                <?php
             endwhile; 
             wp_reset_postdata();
             ?>
          </ol>
       <?php
       endif;
    }
    

    The outfit history meta box uses the name of the closet post to give the correct category to the wp_query to loop in the outfit posts with the client’s name taxonomy. When this happens, it loops in the posts correctly, but all other custom fields within the “closet” post disappear and the closet permalink is reset to one of the outfit post titles.

    However, if I do not try to pass the title of the closet post to the wp_query as the taxonomy term, the custom fields in the closet post work as expected, the permalink is not overwritten as expected, but I lose the loop of the “outfit” post types that are attached to that client via custom tax.

    I do not think I need to create the nonce since I’m not saving any data in this metabox?

    For whatever reason, when trying to use the closet post title to match to the custom tax term, it’s messing up the main page loop I think which is why it causes the custom field values to disappear and the permalink to be overwritten by one of the outfit post titles on save.

Viewing 1 replies (of 1 total)
  • Thread Starter traveler

    (@visualeight)

    This adjustment seems to have fixed the above errros:

    
    <?php
    // ADDS META BOX TO 'CLOSET' SCREEN SHOWING OUTFITS CREATED FOR THIS CLIENT
    
    // ADDS FONT AWESOME TO 'CLOSET' ADMIN SCREEN
    function queue_scripts_to_closet() {
       wp_enqueue_style( 'le-font-awesome-css', 'https://pro.fontawesome.com/releases/v5.10.0/css/all.css' );
    }
    add_action('admin_enqueue_scripts', 'queue_scripts_to_closet');
    
    // CREATE THE META BOX
    function le_outfit_loop_meta_box() {
        $screens = ['closet'];
        foreach ($screens as $screen) {
            add_meta_box(
                'outfit-loop-meta-wrapper',      // Unique ID
                'Outfit History',                // Box title
                'le_outfit_loop_html',           // Content callback function, must be of type callable
                $screen,
                'side'
            );
        }
    }
    add_action('add_meta_boxes_closet', 'le_outfit_loop_meta_box');
    
    // HTML TO RENDER THE META BOX
    function le_outfit_loop_html($post) {
       
       global $post;
       $tempPostVar = $post;
       $editScreenId = $post->ID;
       $clientTaxTerm = $post->post_title;
       echo "<h4>" . $clientTaxTerm . "'s Outfits:</h4>";
       ?>
       
       <div class="admin-button-wrapper">
          <a href="/wp-admin/post-new.php?post_type=outfit" 
             id="js-create-outfit-btn" 
             class="btn">
             <i class="fas fa-plus-circle">&nbsp;</i>
             Create New Outfit
          </a>
       </div>
    
       <?php
       $outfitLoopArgs = array( 
          'post_type' => 'outfit',
          'tax_query' => array(
             array(
                'taxonomy'  => 'client',
                'field'     => 'slug',
                'terms'     => $clientTaxTerm, // should equal the closet post title
             ),
          ),
       ); 
    
       $outfits = get_posts( $outfitLoopArgs ); 
    	if ($outfits) : ?>
          <ol>
          <?php
          foreach ($outfits as $outfit): setup_postdata( $outfit );
             ?>
             <li>
                <a target="_blank" href="<?php echo get_edit_post_link($outfit->ID); ?>"><?php echo $outfit->post_title; ?></a>
             </li>
             <?php
          endforeach;
          // wp_reset_postdata();
          $post = $tempPostVar;
          ?>
          </ol>
          <?php
       endif;
    }
    
Viewing 1 replies (of 1 total)
  • The topic ‘Custom meta box with loop for post edit screen’ is closed to new replies.