Support

Account

Home Forums Front-end Issues Query posts for a relationship value fallback issues Reply To: Query posts for a relationship value fallback issues

  • Thanks for the suggestion, I resolved this by merging the two queries.

    
    function get_post_primary_category($post_id, $term='category', $return_all_categories=false){
        $return = array();
    
        if (class_exists('WPSEO_Primary_Term')){
            // Show Primary category by Yoast if it is enabled & set
            $wpseo_primary_term = new WPSEO_Primary_Term( $term, $post_id );
            $primary_term = get_term($wpseo_primary_term->get_primary_term());
    
            if (!is_wp_error($primary_term)){
                $return['primary_category'] = $primary_term;
            }
        }
    
        if (empty($return['primary_category']) || $return_all_categories){
            $categories_list = get_the_terms($post_id, $term);
    
            if (empty($return['primary_category']) && !empty($categories_list)){
                $return['primary_category'] = $categories_list[0];  //get the first category
            }
            if ($return_all_categories){
                $return['all_categories'] = array();
    
                if (!empty($categories_list)){
                    foreach($categories_list as &$category){
                        $return['all_categories'][] = $category->term_id;
                    }
                }
            }
        }
    
        return $return;
    }
    
    $args = array (
        'post_type' => 'article',
        'orderby' => 'posted_date',
        'order' => 'DESC',
        'post_status' => 'publish',
        'posts_per_page' => $totalRecordsToDisplay,
        'meta_query' => array (
            array(
                'key' => 'authors',
                'value' => '"' . get_the_ID() . '"',
                'compare' => 'LIKE'
            )  
        )
    );
    
    $articles = new WP_Query( $args );
    
    if ($articles->post_count < $totalRecordsToDisplay ) {
    
        $new_TotalRecordsToDisplay = $totalRecordsToDisplay - $articles->post_count;
        $article_expertise = get_post_primary_category($post->ID, 'expertise');
        $primary_expertise = $article_expertise['primary_category'];
    
        //echo $primary_expertise->term_id;
        $args = array(
            'post_type' => 'article',
            'orderby' => 'posted_date',
            'order' => 'DESC',
            'post_status' => 'publish',
            'posts_per_page' => $new_TotalRecordsToDisplay,
            'tax_query' => array(
                array(
                    'taxonomy' => 'expertise',
                    'field' => 'term_id',
                    'terms' => $primary_expertise->term_id,
                    //'terms' => array(join(",", wp_list_pluck($expertises, 'term_id'))),
                    'include_children' => true
                )
            )
        );
        $more_query = new WP_Query( $args );
        $articles->posts = array_merge( $articles->posts, $more_query->posts );
        $articles->post_count = count( $articles->posts );
    }