Custom Fields
Re-Envisioned
Support MB Custom Table Create custom taxonomy archive template

  • Creator
    Topic
  • #21184
    Resolved rebecmari
    Participant

    Hello!

    I am working on creating custom templates for a records database using MB Custom Table. I have created a custom post type with custom taxonomies for the records and am able to display all data correctly in an archive-cemetery-records.php template.

    I am now trying to set up the template for the custom taxonomy and have created taxonomy-cemetery-records-cemeteries.php. I have put the archive-cemetery-records.php template code (added below), into this file, but am unsure how to filter the data in the template by taxonomy, so only the records for that taxonomy will display. Right now (as expected since I just copied the same code), the full records list is displaying

    I have found some information about adding a 'tax_query' => array() to $args. If this is what I should do, how to I reference the custom table field? If this isn’t correct, can you point me in the right direction on how to filter the data?

    Thank you!

    <?php
    /*Template Name: Cemetery Records */
    get_header();
    
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $args = array( 'post_type' => 'cemetery-records', 'posts_per_page'=>2, 'orderby'=>'last_name','order'=>'ASC', 'paged' => $paged);
    $loop = new WP_Query( $args )
    
    ?>
    
    <div class="table-1">
        <table width="100%">
            <thead>
                <tr>
                    <th>Headstone</th>
                    <th>Last Name</th>
                    <th>First Name</th>
                    <th>Middle Name</th>
                    <th>Cemetery</th>
                    <th>More Info</th>
                </tr>
            </thead>
            
            <tbody>
            <?php while ($loop->have_posts() ) : $loop->the_post(); 
            $table_name = 'my_custom_table';    
            $first_name = rwmb_meta( first_name, ['storage_type' => 'custom_table', 'table' => $table_name] );
            $middle_name = rwmb_meta( middle_name, ['storage_type' => 'custom_table', 'table' => $table_name] );
            $last_name = rwmb_meta( last_name, ['storage_type' => 'custom_table', 'table' => $table_name] );  
            $main_image = rwmb_meta( main_image , ['storage_type' => 'custom_table', 'table' => $table_name]);
            $term = rwmb_meta( 'cemetery_records_cemeteries' );
            $cemetery_records_cemeteries = rwmb_meta( cemetery-records-cemeteries, ['storage_type' => 'custom_table', 'table' => $table_name] );      
            ?>     
                <tr>
                    <td>
                        <?php if (!empty($main_image)) {
                            $img_id = attachment_url_to_postid( $main_image );                    
                            echo wp_get_attachment_image( $img_id, $size = 'thumbnail' );
                        } ?>
                    </td>
                    <td><?php echo $last_name; ?></td>
                    <td><?php echo $first_name; ?></td>
                    <td><?php echo $middle_name; ?></td>
                    <td>
                        <?php $cemetery = rwmb_meta('cemetery-records-cemeteries', ['storage_type' => 'custom_table', 'table' => $table_name] );
    
                        if (!empty($cemetery)) {
                            foreach ($cemetery as $term) {
                                $cemetery_name = $term->name;
                                $cemetery_link = get_term_link( $term );
                            }
                            echo '<a href="' . $cemetery_link . '">'. $cemetery_name . '</a>';
                        } ?>
                    </td>
                    <td>
                        <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">View Record</a>
                    </td>
                </tr>
            <?php endwhile; ?>
            </tbody>
        </table>
    </div>
    
    <nav class="pagination">
         <?php
         $big = 999999999;
         echo paginate_links( array(
              'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
              'format' => '?paged=%#%',
              'current' => max( 1, get_query_var('paged') ),
              'total' => $loop->max_num_pages,
              'prev_text' => '&laquo;',
              'next_text' => '&raquo;'
         ) );
    ?>
    </nav>
    <?php wp_reset_postdata(); ?>
    
    <?php
    get_footer();
Viewing 2 replies - 1 through 2 (of 2 total)
  • Author
    Replies
  • #21191
    Long Nguyen
    Moderator

    Hi,

    If you use the custom table to save the custom field’s value, here is the sample query to filter the post base on the field’s value https://docs.metabox.io/extensions/mb-custom-table/#query-posts-with-wp_query.

    #23791
    rebecmari
    Participant

    Thank you. This is resolved. I was able to modify this to work for me. Sample of the code is below, in case it is helpful to anyone:

    
    <?php
    global $wpdb;
    $term_id = get_queried_object_id();
    $ids = $wpdb->get_col( "SELECT ID FROM my_custom_table WHERE cemetery_records_cemeteries = $term_id" );
    
    $args = array( 
        'post_type' => 'cemetery-records',
        'post__in'  => $ids,
        'posts_per_page'=>20, 
        'orderby'=>'last_name',
        'order'=>'ASC', 
        'paged' => $paged
    );
    $loop = new WP_Query( $args )
    ?>
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.