skip to Main Content

I’m using custom post types UI plugin on my website.

Now I have blog posts ( default wp posts – news ) and a new custom post type ( articles ).

I want to show all the posts for these two post types on another page.
but I can not combine this two together!

Here is what i’m using to show articles posts:

public function News() {
  $terms = get_terms(['post_type' => 'articles','taxonomy' => "article_category",'hide_empty' => false,]);

2

Answers


  1. Hi if WP Query does not work for you, you can try and get the id’s of these two types. Then make a function that pulls the information that you need from each one based on their id’s.

    global $wpdb;
    
    $ids = $wpdb->get_results(" SELECT `ID` FROM `wp_posts` WHERE `post_type` LIKE 'post' OR `post_type` LIKE 'articles'  ";
    
    foreach($ids as $id){
    //use another custom function to get the ACF or postmeta whatever it is you need
    }
    
    Login or Signup to reply.
  2. This could easily be done by using wp_query. Please avoid writing your own sql queries as much as possible because these custom sql queries would potentially make you vulnerable to slq attacks. You could combine your post types like this 'post_type' => array( 'post', 'articles' ).

    $args = array(
      'post_type'      => array( 'post', 'articles' ),
      'posts_per_page' => -1
    );
    
    $custom_query = new WP_Query( $args );
    
    if ($custom_query) {
      while ($custom_query->have_posts()) {
        $custom_query->the_post(); ?>
        <a href="<?php the_permalink(); ?>">
          <h4><?php the_title(); ?></h4>
        </a>
    <?php
      }
    }
    
    wp_reset_postdata();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search