V
V
Vladislav Ignatiev2018-03-12 14:42:11
WordPress
Vladislav Ignatiev, 2018-03-12 14:42:11

How to display posts whose title slug is similar to the category slug of other posts, playing with variables?

Good afternoon!
I have a single.php page with a similar structure inside.

$company_pname = wp_get_post_categories( $post->ID, array('fields' => 'all') );
  foreach( $company_pname as $cat_comp ){
  $cat_comp->slug;
  }
      $args = array( 'post_type' => 'post-pharmacy', 'posts_per_page' => 999999, 'name' => $cat_comp->slug );
      $query_company_name = new WP_Query($args);
      if ($query_company_name->have_posts()) {
        while ($query_company_name->have_posts()) {
          $query_company_name->the_post();?>
          
          <h1><?php the_title(); ?></h1>
          
        <?php }
      }
      wp_reset_postdata();

As you can see, I get from this post all the slugs of the category that this post belongs to and write it to a variable $cat_comp->slugfurther, this variable, which stores only slugs, I use in the request in $argsnamely, 'name' => $cat_comp->slugand then I need it to display each post whose slugs are the title , specified in args.
Now it displays only 1 post, since only 1 category slug is passed to name, how to make a loop or some kind of array so that all slugs are passed to name, and not just 1 ???
Please help, what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question