การจัดหมวดหมู่กลุ่ม WordPress สำหรับโพสต์ตามความสัมพันธ์ของ ACF

ฉันมีสองประเภทโพสต์: Teachers และ Students ข้อมูลตัวอย่าง:

ประเภทกระทู้ของครู:

Teacher A
Teacher B
Teacher C 

และประเภทการโพสต์ของนักเรียน:

Student A
Student B
Student C
and many more students

ฉันยังมีอนุกรมวิธานที่เรียกว่า student-role ซึ่งมีค่าบางอย่าง:

Intern
Master Student
PhD Student

ในประเภทโพสต์ของนักเรียน ฉันใช้ความสัมพันธ์ ACF เพื่อมอบหมายครูให้กับนักเรียน นักเรียนสามารถมีครูได้มากกว่าหนึ่งคน

ตัวอย่างเช่น:

  • Student A คือ Intern และถูกกำหนดให้เป็น Teacher A และ Teacher B
  • Student B คือ Master Student และถูกกำหนดให้เป็น Teacher C
  • Student C คือ PhD Student และถูกกำหนดให้เป็น Teacher C

ฉันต้องการสร้างเพจแบบกำหนดเองสำหรับ teacher C โดยที่ฉันแสดงเฉพาะ student-roles ทั้งหมดเท่านั้น

จึงมีหน้าสำหรับครู C และควรแสดงรายการ Master Student และ PhD Student เนื่องจากมีนักเรียนสองคนภายใต้อนุกรมวิธานนั้น

ฉันพยายามดึงนักเรียนทั้งหมดก่อน จากนั้นจึงดึงบทบาทของนักเรียนทั้งหมดแต่ไม่แน่ใจว่าต้องทำอย่างไรต่อไป มีแนวคิดอะไรบ้าง

$args = array(
    'posts_per_page' => -1,
    'post_type'  => 'students',
    'meta_query' => array(
        array(
            'key'     => 'acf_relationship',
            'value'   => 'Manual ID OF Teacher',
            'compare' => 'LIKE'
        )
    )
);
// get all posts for teacher with ID
$all_posts = get_posts( $args );

// get all student roles
$tax_terms = get_terms( 'student-role');

person user5783940043    schedule 27.04.2019    source แหล่งที่มา
comment
แม้ว่าฉันจะไม่มีคำตอบสำหรับคุณในตอนนี้ แต่ฉันคงไม่กล้าใช้ meta_query สำหรับสิ่งนี้ หากคุณใช้สิ่งนี้ในวงกว้าง นักเรียนและครูไม่กี่ร้อยคนก็ถือว่าไม่เลว แต่ meta_queries อาจทำงานช้าอย่างเจ็บปวดในชุดข้อมูลขนาดใหญ่ เนื่องจากฟิลด์ meta_value ไม่ได้รับการจัดทำดัชนีในฐานข้อมูล   -  person Xhynk    schedule 27.04.2019


คำตอบ (2)


เพจที่คุณกำหนดเองชื่อ single-teacher.php หรือไม่

  1. ขั้นแรก คุณต้องสร้าง single-teacher.php ซึ่งคุณจะแสดงหน้าของครู
  2. ภายในหน้าของครู คุณจะต้องได้รับวัตถุของนักเรียนโดยใช้ความสัมพันธ์ ACF
  3. คุณจะมีบัตรประจำตัวนักเรียนอยู่ภายในวัตถุนั้น จากนั้นคุณสามารถรับอนุกรมวิธานได้อย่างง่ายดาย

ตรวจสอบรหัสนี้

<?php
// the get_the_ID() will get the teacher's ID since you are in single-teacher.php
// $the_post will be the student object

// $the_posts is the student's object
$the_posts = get_field('acf_relationship', get_the_ID()); 

if( $the_posts ): ?>
 <ul>
   <?php foreach( $the_posts as $p ): // variable must NOT be called $post ?>
    <li>
        <!-- This will display the student title or name -->
        <a href="/th<?php echo get_permalink( $p->ID ); ?>"><?php echo get_the_title( 
           $p->ID ); ?></a> 

        <!-- This will display the student's taxononmy -->
        <?php
        $the_terms = get_the_terms( $p->ID, 'your_taxonomy_name' );
        $t_url = '';
        $t_name = '';
        foreach ($the_terms as $t) {
            $t_url = get_term_link( $t->slug, 'your_taxonomy_name');
            $t_name = $t->name;
        }
        ?>
        <!-- Print student role list with link -->
        <span>Your student roles: <a href="/th<?php echo $t_url; ?>">
           <?php echo $t_name; ?></a>
        </span>
    </li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
person Romel Indemne    schedule 27.04.2019

หมายเหตุ: ฟิลด์ ACF ที่กำหนดให้กับนักเรียนควรส่งคืนเฉพาะ Post id ไม่ใช่วัตถุโพสต์ (คุณสามารถตั้งค่าได้โดยแก้ไขฟิลด์ acf)

ที่นี่แบบสอบถามสำหรับครู C (รหัสโพสต์ของครู c = 1662)

$args = array(
    'numberposts'   => -1,
    'post_type'     => 'student',
);
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
    <ul>
    <?php while( $the_query->have_posts() ) : $the_query->the_post(); 
        if( in_array('1662', get_field('assign')) ) : ?>
        <li>
            <a href="/th<?php the_permalink(); ?>">                
                <?php the_title(); ?>
            </a>
        </li>
        <?php endif; 
    endwhile; ?>
    </ul>
<?php endif; ?>
<?php wp_reset_query();   ?>

ในขณะที่วนซ้ำ คุณสามารถแสดงชื่อ บทบาทเป็นอนุกรมวิธาน เนื้อหา ฯลฯ

person Chirag Rathore    schedule 27.04.2019