How can i get related posts from all the custom post types based on custom taxonomy of particular post in WordPress?

Schema for posts has more arguments that are useful for filtering posts by category name or category id. I've registered multiple custom post types and they are missing those arguments?

How can I add/register more schema arguments with PHP for my custom post types in the WP Graphql? WP GraphQL docs don't cover this particular feature.

Any kind of advice/help will be great. Thanks in advance.

Please see the screenshot comparison for posts and custom post types.

Posts:

enter image description here

Custom Post Type:

enter image description here

Comments


  • Estrus
    <?php
    //Get array of terms
    $terms = get_the_terms($post->ID, 'categories--blog', 'string');
    //Pluck out the IDs to get an array of IDS
    $term_ids = wp_list_pluck($terms, 'term_id');
    
    //Query posts with tax_query. Choose in 'IN' if want to query posts with any of the terms
    //Chose 'AND' if you want to query for posts with all terms
    $second_query = new WP_Query(array(
        'post_type' => 'blog',
        'tax_query' => array(
            array(
                'taxonomy' => 'categories--blog',
                'field' => 'id',
                'terms' => $term_ids,
                'operator' => 'IN' //Or 'AND' or 'NOT IN'
            )
        ),
        'posts_per_page' => 3,
        'ignore_sticky_posts' => 1,
        'orderby' => 'rand',
        'post__not_in' => array($post->ID)
    ));
    
    //Loop through posts and display...
    if ($second_query->have_posts()) { ?>
        <div class="related-blog-section">
            <div class=" container-1280">
                <h2>Related —</h2>
                <ul class="flex-wrap">
                    <?php
                    while ($second_query->have_posts()) : $second_query->the_post(); ?>
                        <li>
                            <div class="black-tag">
                                <?php
                                $relatedpostType = get_post_type_object(get_post_type($post));
                                if ($relatedpostType) {
                                    echo esc_html($relatedpostType->labels->singular_name);
                                }
                                ?>
                            </div>
                            <?php if (has_post_thumbnail()) { ?>
                                <div class="image-section">
                                    <a href="<?php the_permalink(); ?>">
                                        <img src="<?php the_post_thumbnail_url(); ?>" alt="<?php the_title(); ?>">
                                    </a>
                                </div>
                            <?php }  ?>
                            <div class="content">
                                <div class="blog-meta">
                                    <span><?php the_taxonomies(); ?></span>
                                    <span class="divider">/</span>
                                    <span class="date"><?php the_time(get_option('date_format')); ?></span>
                                </div>
                                <a href="<?php the_permalink(); ?>" class="title"><?php the_title(); ?> </a>
                            </div>
                        </li>
                    <?php endwhile;
                    wp_reset_query(); ?>
                </ul>
            </div>
        </div>
    <?php
    }
    ?>
    

    I have this code where I can fetch related posts from the single taxonomy and single post type instead I want to fetch all the posts related to the parent post taxonomy.

    For example, i am on the blog details page of a particular blog and it has the custom category "abc" now I would like to fetch all the posts which have the custom category "abc" from all the custom posts available in admin not just the one blog post.

Add Comment