1

I have Two Custom Post Types "Article " & "News" and they both use same categories like [ audit , income , tax] . I want when i open audit category page then the post showsn like below: enter image description here

the code i already tried on archieve.php but isnt working:

<?php

    $cats = get_the_category();
    $args = array(
        'post_type' => 'articles',
        'post__not_in' => array( get_the_ID() ),
        'posts_per_page' => 5,
        'cat'     => $cats[0]->term_id,
        'meta_query' => array(
                    array(
                    'key' => 'recommended_article',
                    'value' => '1',
                    'compare' => '=='
                        )
                    )
    );
    $query = new WP_Query( $args );

?>  

<?php if( $query->have_posts() ) : while( $query->have_posts() ) : $query->the_post(); ?>   

<!--HTML-->

<?php endwhile; endif; wp_reset_postdata(); ?>
1

First, I would recommend making this change in Category.php file, as archive.php is affecting tags as well as other archives according to Wordpress Theme Hierarchy file system.

Try adding the two queries like next:

$cats = get_the_category();
    $cat_id = $cats[0]->term_id;


        // news
        $args = array(
    'post_type' => 'news',
    'posts_per_page' => 5,
    'cat'     => $cat_id,
);
$query = new WP_Query($args);

if($query->have_posts() ) : 
    while( $query->have_posts() ) :
        $query->the_post();
    endwhile; 
endif; 

wp_reset_postdata(); 

    // articles
$args = array(
    'post_type' => 'articles',
    'posts_per_page' => 5,
    'cat'     => $cat_id,
);
$query = new WP_Query($args);

if($query->have_posts() ) : 
    while( $query->have_posts() ) :
        $query->the_post();
    endwhile; 
endif; 

wp_reset_postdata(); 
| improve this answer | |
  • Tried !! its showing Blank Page – Hakimuddin Saifee Dec 12 '19 at 12:22
  • Please set WP_DEBUG to true in your wp-config.php file and refresh the page to get the errors and write them here :) – Heba Fareed Dec 12 '19 at 12:24
1

Create taxonomy-categories.php or In your archive.php add below code to get post of article and news post type for current category. I assume that your taxonomy for both post type is categories. change categories with your taxonomy and post_type with your post types for news and article from below code.

 # Article 
 $articleargs = array(
        'post_type' => 'article',
        'posts_per_page' => 5,
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'categories',
                'field'    => 'name',
                'terms'    => get_queried_object()
            )
        ),
    );
    $articlequery = new WP_Query( $articleargs );

     if( $articlequery->have_posts() ) : 
        echo "<h2>Articles</h2>";
        echo "<ul>";
        while( $articlequery->have_posts() ) : $articlequery->the_post();   
            echo "<li>".get_the_title()."</li>";
         endwhile; 
         echo "</ul>";
    endif; 
    wp_reset_postdata(); 

    # news
    $newsargs = array(
        'post_type' => 'news',
        'posts_per_page' => 5,
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'categories',
                'field'    => 'name',
                'terms'    => get_queried_object()
            )
        ),
    );
    $newsquery = new WP_Query( $newsargs );

     if( $newsquery->have_posts() ) : 
        echo "<h2>News</h2>";
        echo "<ul>";
        while( $newsquery->have_posts() ) : $newsquery->the_post();   
            echo "<li>".get_the_title()."</li>";
         endwhile; 
         echo "</ul>";
    endif; 
    wp_reset_postdata(); 
| improve this answer | |
1

The get_the_category() function retrieves the categories assigned to the first post from the loop, and that's not what you mean. I assume that audit, income, tax, etc. are built-in categories, not custom taxonomy (in this case you need to replace cat parameter of WP_Query to tax_query).

Create category.php file if it doesn't exist by copying archive.php. This way the template will be used only on the category page.

ID of the currently viewed category you can get with get_queried_object_id(). You don't have to check type of the queried object in the category.php file, it will always be correct. Having a category ID, you insert them into the query parameters and you only receive posts belonging to it.

If you intend to apply the template only to selected categories (audit, income, tax) instead of all use the {$type}_template filter.

//
// ge ID of the  current category
$cat_id = get_queried_object_id();

//
// get 'news' posts from current category 
$args = array(
    'post_type' => 'news',
    'posts_per_page' => 10,
    'cat'       => (int)$cat_id,  // <==
    //'meta_query' => array(
    //    array(
    //        'key' => 'recommended_article',
    //        'value' => '1',
    //        'compare' => '=',    // <== 
    //    )
    )
);
$query = new WP_Query( $args );
//
// display posts
// 
wp_reset_postdata();


//
// get 'articles' posts from current category 
$args = array(
    'post_type' => 'articles',   // <==
    'posts_per_page' => 10,
    'cat'       => (int)$cat_id,
    //'meta_query' => array(
    //    array(
    //        'key' => 'recommended_article',
    //        'value' => '1',
    //        'compare' => '=',
    //    )
    )
);
$query = new WP_Query( $args );
//
// ...
// 
wp_reset_postdata();
| improve this answer | |
0

Its Done Thank U so much Everyone Who Answered!

<?php
/*
 * dglib_breadcrumbs_section_template - hook
 *
 * @hooked - dglib_breadcrumbs_section_callback - 10
 *
 * @since 1.0.6
 */

do_action( 'dglib_breadcrumbs_section_template' );
?>
    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">


<header class="page-header">
                <?php
                    the_archive_title( '<h1 class="page-title">', '</h1>' );
                    the_archive_description( '<div class="archive-description">', '</div>' );

                ?>
            </header>
<div class="row">
                <?php

$category = get_queried_object();

        // news
        $args = array(
    'post_type' => 'news',
    'posts_per_page' => 5,
    'cat'     =>  $category->term_id,
);

$the_query = new WP_Query( $args ); 
?>
<?php if ( $the_query->have_posts() ) : ?>
            <!-- .page-header -->

<div class="col-md-6 col-sm-6">
            <div class="panel panel-default"> 
            <div class="panel-heading">
        <h3 class="sub_header">News</h3>
        </div>
        <ul style="padding-left: 25px; padding-top:15px;list-style: disc; line-height:23px">
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php the_title( '<li class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></li>' ); ?>

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

<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif;  ?>
<?php



        // articles
        $args1 = array(
    'post_type' => 'articles',
    'posts_per_page' => 5,
    'cat'     =>  $category->term_id,
);

$the_query1 = new WP_Query( $args1 ); 
?>
<?php if ( $the_query1->have_posts() ) : ?>
            <!-- .page-header -->

<div class="col-md-6 col-sm-6">
            <div class="panel panel-default"> 
            <div class="panel-heading">
    <h3 class="sub_header">Articles</h3>
        </div>
        <ul style="padding-left: 25px; padding-top:15px;list-style: disc; line-height:23px">
<?php while ( $the_query1->have_posts() ) : $the_query1->the_post(); ?>
<?php the_title( '<li class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></li>' ); ?>

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

<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif;  ?>
</div>
        </main><!-- #main -->
    </div><!-- #primary -->
<?php
get_sidebar();
get_footer();

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.