admin管理员组

文章数量:1204600

We have a most recent block where we query a taxonomy for a custom post type and posts from two custom post types. This we do using this code

Full Code

<?php
/**
 * Highlights block
 *
**/
// Create id attribute allowing for custom "anchor" value.
$id = 'highlights-' . $block['id'];
if( !empty($block['anchor']) ) {
    $id = $block['anchor'];
}

// Create class attribute allowing for custom "className" and "align" values.
$className = 'block-highlights ';
if( !empty($block['className']) ) {
    $className .= ' ' . $block['className'];
}
if( !empty($block['align']) ) {
    $className .= ' align' . $block['align'];
}

$taxonomy = 'molecules_of_the_month';
$parent_args = [
    'taxonomy'      => $taxonomy,
    'parent'        => 0,
    'hide_empty'    => false,
    'number'        => 1,
    'fields'        => 'ids',
];
//get the first parent year
$parent_terms = get_terms( $parent_args );
//print_r($parent_terms);

$child_args = [
    'taxonomy'      => $taxonomy,
    'parent'        => $parent_terms[0],
    'hide_empty'    => false,
    'number'        => 1,
    'fields'        => 'ids',
];
//get the children
$child_terms = get_terms( $child_args );
//print_r($child_terms);


//setup the queries getting only ids
$query1 = new WP_Query(array('fields' => 'ids', 'post_type' => 'molecule', 'posts_per_page' => 2 ));
$query2 = new WP_Query(array('fields' => 'ids', 'post_type' => 'resource', 'posts_per_page' => 2));
$query3 = new WP_Query(array('fields' => 'ids', 'posts_per_page' => 3 ));


//now you got post IDs in $query->posts
$merged_ids = array_merge($query1->posts, $query2->posts, $query3->posts);

//new query, using post__in parameter
$highlights = new WP_Query(array('post__in' => $merged_ids, 'post_type' => array('post', 'molecule', 'resource') ));

?>

<div id="<?php echo esc_attr($id); ?>" class="<?php echo esc_attr($className); ?>">
<?php if($highlights): ?>
<div class="slider mobile-slider splide" data-splide='{ "type" : "loop", "perPage": "3", "arrows": 0, "breakpoints": { "99999": { "destroy": 1 }, "767": { "perPage": "1" } } }'>
      <div class="splide__track">
<ul class="post-grid splide__list">
    <?php
    //output MOTM
    if($child_terms):
    foreach ( $child_terms as $child_term ) {
        //get term object
        $term = get_term_by( 'id', $child_term, $taxonomy ); 
        //print_r($term);
    ?>
    <li class="post-grid-post splide__slide">
        <a href="<?php echo get_term_link($term); ?>">
            <?php 
            //get featured image
            $featured_image = get_field('tax_featured_image', $term);
            if( $featured_image ) {               
            ?>
            <figure class="post-featured-image">
                <?php  echo wp_get_attachment_image( $featured_image, 'full' ); ?>
            </figure>
            <?php
            }
            ?>
            <ul class="cat-pill-list">
                <li class="cat-pill" style="background: #38E8AD;">
                    Molecules of the Month
                </li>
            </ul>
            <h2 class="post-grid-title"><?php echo $term->name; ?></h2>
    <?php
        $description = get_term_meta( $child_term, 'intro_text', true );
        
        if($description){
        ?>
        <p> <?php echo wp_trim_words($description, 19); ?></p>
        <?php
        }else{
                echo wp_trim_words(term_description($term), 19 ); 
        }
    ?>
            <p class="reading-time"><?php echo dh_display_read_time(); ?></p>
        </a>
    </li>
    <?php
    }
    endif;
    ?>
<?php while( $highlights->have_posts() ) : $highlights->the_post(); ?>
<!-- Post Content goes here -->
    <li class="post-grid-post splide__slide">
        
        <a href="<?php the_permalink(); ?>">
            <figure class="post-featured-image"><?php the_post_thumbnail(); ?></figure>
            <?php
           $post_id = get_the_ID();
            if ( get_post_type( $post_id ) == 'resource' ) {
                $cats = get_the_terms( $post_id, 'resource_category');
                if($cats):
        ?>
        <ul class="cat-pill-list">
        <?php
                foreach ( $cats as $cat):
                    
                    $category_color = get_field('category_color', $cat);
                    $cat_text_color = get_field('cat_text_color', $cat);
        ?>
            <li class="cat-pill <?php echo $cat_text_color; ?>" style="background: <?php echo $category_color; ?>;">
                <?php echo $cat->name; ?>
            </li>
        <?php
                endforeach;
        ?>
        </ul>
        <?php
                endif;
            }elseif( get_post_type( $post_id ) == 'molecule' ){
        ?>
            <ul class="cat-pill-list">
                <li class="cat-pill" style="background: #38E8AD;">
                    Molecules of the Month
                </li>
            </ul>
        <?php        
            }else{
        ?>
            <ul class="cat-pill-list">
                <li class="cat-pill light" style="background: #EB546B">
                    Article
                </li>
            </ul>
        <?php        
            }
        ?>
            <h2 class="post-grid-title"><?php the_title(); ?></h2>
            <?php echo dh_excerpt(); ?>
            <p class="reading-time"><?php echo dh_display_read_time(); ?></p>
        </a>
    </li>

<?php endwhile; wp_reset_postdata();?>
</ul>
      </div>
</div>
<?php endif; ?>
</div>

But we want to mix $hightlights and filter the taxonomy, two custom post types by most recent, not do these three separately in their own content boxes by date.

We could add an ACF date picker field so we can sort taxonomies by a chosen date

'meta_key' => 'month_date', // name of custom field
'orderby' => 'meta_value_num',
'order' => 'ASC',

but that is only the start.

Clarifications

Child terms are the months which are parents of years. Each child term has a number of custom post type posts (molecules).

$highlights should show a most recent block with a mix of articles (posts), resources (custom post type posts) and child terms or molecules of the month and all in chronological order. A most recent block basically showing the latest resource articles, general articles and child therms and two of each. Currently we always get child terms first and then the content posts. And we want them to truly be mixed and loading in ascending order.

Question

How can we do this? How do you use this merged array and then query the latest two of each and mix them all up and sort ascending (latest first)?

Example

Ascending Order:

  1. molecule 31/03/2022 (cpt)
  2. molecule of the month March 2022 (child term of year 2022)
  3. resource 28/03/2022 (cpt)
  4. resource 27/03/2022 (cpt)
  5. article 26/03/2022 (post)
  6. molecule of the month February 2022 (child term of year 2022)
  7. article 20/03/2022 (post)

We have a most recent block where we query a taxonomy for a custom post type and posts from two custom post types. This we do using this code

Full Code

<?php
/**
 * Highlights block
 *
**/
// Create id attribute allowing for custom "anchor" value.
$id = 'highlights-' . $block['id'];
if( !empty($block['anchor']) ) {
    $id = $block['anchor'];
}

// Create class attribute allowing for custom "className" and "align" values.
$className = 'block-highlights ';
if( !empty($block['className']) ) {
    $className .= ' ' . $block['className'];
}
if( !empty($block['align']) ) {
    $className .= ' align' . $block['align'];
}

$taxonomy = 'molecules_of_the_month';
$parent_args = [
    'taxonomy'      => $taxonomy,
    'parent'        => 0,
    'hide_empty'    => false,
    'number'        => 1,
    'fields'        => 'ids',
];
//get the first parent year
$parent_terms = get_terms( $parent_args );
//print_r($parent_terms);

$child_args = [
    'taxonomy'      => $taxonomy,
    'parent'        => $parent_terms[0],
    'hide_empty'    => false,
    'number'        => 1,
    'fields'        => 'ids',
];
//get the children
$child_terms = get_terms( $child_args );
//print_r($child_terms);


//setup the queries getting only ids
$query1 = new WP_Query(array('fields' => 'ids', 'post_type' => 'molecule', 'posts_per_page' => 2 ));
$query2 = new WP_Query(array('fields' => 'ids', 'post_type' => 'resource', 'posts_per_page' => 2));
$query3 = new WP_Query(array('fields' => 'ids', 'posts_per_page' => 3 ));


//now you got post IDs in $query->posts
$merged_ids = array_merge($query1->posts, $query2->posts, $query3->posts);

//new query, using post__in parameter
$highlights = new WP_Query(array('post__in' => $merged_ids, 'post_type' => array('post', 'molecule', 'resource') ));

?>

<div id="<?php echo esc_attr($id); ?>" class="<?php echo esc_attr($className); ?>">
<?php if($highlights): ?>
<div class="slider mobile-slider splide" data-splide='{ "type" : "loop", "perPage": "3", "arrows": 0, "breakpoints": { "99999": { "destroy": 1 }, "767": { "perPage": "1" } } }'>
      <div class="splide__track">
<ul class="post-grid splide__list">
    <?php
    //output MOTM
    if($child_terms):
    foreach ( $child_terms as $child_term ) {
        //get term object
        $term = get_term_by( 'id', $child_term, $taxonomy ); 
        //print_r($term);
    ?>
    <li class="post-grid-post splide__slide">
        <a href="<?php echo get_term_link($term); ?>">
            <?php 
            //get featured image
            $featured_image = get_field('tax_featured_image', $term);
            if( $featured_image ) {               
            ?>
            <figure class="post-featured-image">
                <?php  echo wp_get_attachment_image( $featured_image, 'full' ); ?>
            </figure>
            <?php
            }
            ?>
            <ul class="cat-pill-list">
                <li class="cat-pill" style="background: #38E8AD;">
                    Molecules of the Month
                </li>
            </ul>
            <h2 class="post-grid-title"><?php echo $term->name; ?></h2>
    <?php
        $description = get_term_meta( $child_term, 'intro_text', true );
        
        if($description){
        ?>
        <p> <?php echo wp_trim_words($description, 19); ?></p>
        <?php
        }else{
                echo wp_trim_words(term_description($term), 19 ); 
        }
    ?>
            <p class="reading-time"><?php echo dh_display_read_time(); ?></p>
        </a>
    </li>
    <?php
    }
    endif;
    ?>
<?php while( $highlights->have_posts() ) : $highlights->the_post(); ?>
<!-- Post Content goes here -->
    <li class="post-grid-post splide__slide">
        
        <a href="<?php the_permalink(); ?>">
            <figure class="post-featured-image"><?php the_post_thumbnail(); ?></figure>
            <?php
           $post_id = get_the_ID();
            if ( get_post_type( $post_id ) == 'resource' ) {
                $cats = get_the_terms( $post_id, 'resource_category');
                if($cats):
        ?>
        <ul class="cat-pill-list">
        <?php
                foreach ( $cats as $cat):
                    
                    $category_color = get_field('category_color', $cat);
                    $cat_text_color = get_field('cat_text_color', $cat);
        ?>
            <li class="cat-pill <?php echo $cat_text_color; ?>" style="background: <?php echo $category_color; ?>;">
                <?php echo $cat->name; ?>
            </li>
        <?php
                endforeach;
        ?>
        </ul>
        <?php
                endif;
            }elseif( get_post_type( $post_id ) == 'molecule' ){
        ?>
            <ul class="cat-pill-list">
                <li class="cat-pill" style="background: #38E8AD;">
                    Molecules of the Month
                </li>
            </ul>
        <?php        
            }else{
        ?>
            <ul class="cat-pill-list">
                <li class="cat-pill light" style="background: #EB546B">
                    Article
                </li>
            </ul>
        <?php        
            }
        ?>
            <h2 class="post-grid-title"><?php the_title(); ?></h2>
            <?php echo dh_excerpt(); ?>
            <p class="reading-time"><?php echo dh_display_read_time(); ?></p>
        </a>
    </li>

<?php endwhile; wp_reset_postdata();?>
</ul>
      </div>
</div>
<?php endif; ?>
</div>

But we want to mix $hightlights and filter the taxonomy, two custom post types by most recent, not do these three separately in their own content boxes by date.

We could add an ACF date picker field so we can sort taxonomies by a chosen date

'meta_key' => 'month_date', // name of custom field
'orderby' => 'meta_value_num',
'order' => 'ASC',

but that is only the start.

Clarifications

Child terms are the months which are parents of years. Each child term has a number of custom post type posts (molecules).

$highlights should show a most recent block with a mix of articles (posts), resources (custom post type posts) and child terms or molecules of the month and all in chronological order. A most recent block basically showing the latest resource articles, general articles and child therms and two of each. Currently we always get child terms first and then the content posts. And we want them to truly be mixed and loading in ascending order.

Question

How can we do this? How do you use this merged array and then query the latest two of each and mix them all up and sort ascending (latest first)?

Example

Ascending Order:

  1. molecule 31/03/2022 (cpt)
  2. molecule of the month March 2022 (child term of year 2022)
  3. resource 28/03/2022 (cpt)
  4. resource 27/03/2022 (cpt)
  5. article 26/03/2022 (post)
  6. molecule of the month February 2022 (child term of year 2022)
  7. article 20/03/2022 (post)
Share Improve this question edited Mar 31, 2022 at 1:39 rhand asked Mar 24, 2022 at 5:27 rhandrhand 3742 gold badges14 silver badges31 bronze badges 15
  • Perhaps I have misunderstood the issue - but you can pass multiple conditions to query arguments like post types to a query 'post_type' => array( 'molecule', 'resource' ), See WP_Query Codex Then just sort by date ascending – Bysander Commented Mar 28, 2022 at 13:20
  • @rhand can you elaborate on the "mix $hightlights and filter the taxonomy"? Because if you want the query to always query for 2 posts from each CPT, then your code is already doing that. So maybe you can add the full code you're using and a screenshot of the current "content boxes" (or output) and a mockup of the output you're hoping to have? – Sally CJ Commented Mar 29, 2022 at 0:42
  • 1 Thanks @rhand. And actually, if only you provided that "Example" when I asked for a mockup, you might have already received an answer 2 days ago.. ✌ Anyway, I bet Mort1305 would be able to update his code, so let's wait for that

    本文标签: wp queryMerge 2 custom post type posts and taxonomy terms and sort ascending