0

I need some help, again.

I use this query:

$tax = get_the_terms($id, 'coupon_category');
                    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                    query_posts( array(
                        'post_type' => APP_POST_TYPE,
                        'post_status' => 'publish',
                        'posts_per_page' => 4,
                        'tax_query' => array( 
                        'relation' => 'AND',
                            array(
                            'taxonomy' => 'coupon_category', // Taxonomy
                            'field'    => 'slug',
                            'terms'    => $tax[0]->slug, // Your category slug
                            ),
                            array(
                            'taxonomy' => APP_TAX_STORE,
                            'field'    => 'slug',
                            'terms'    => $term->slug,
                            'operator' => 'NOT IN',
                            ),
                        ),                      
                    ) );

I know need a solution to only show one post from each Taxonomy term APP_TAX_STORE. Tried some solutions from this userfull forum but non worked for me.

1

Yeah sure.

<?php 
                    $included_post_ids =array();
                    $args = array(
                      'post_type' => 'APP_POST_TYPE',
                      'post_status' => 'publish',
                      'posts_per_page' => 1,
                      'tax_query' => array( 

                          array(
                          'taxonomy' => 'coupon_category', // Taxonomy
                          'field'    => 'slug',
                          'terms'    => $tax[0]->slug, // Your category slug
                          ),
                    ),
                  );
                  $custom_posts = get_posts( $args );
                  if( !empty( $custom_posts ) ){
                    foreach ($custom_posts as $key => $postObj) {
                      $included_post_ids[] = $postObj->ID;
                    }
                  }
                  //Similar proceduce for other taxonomies 
                  $args = array(
                    'post__in' => $included_post_ids,
                    'post_type' => 'APP_POST_TYPE',
                  )
                  $final_posts = new WP_Query( $args );
                  if ( $final_posts->have_posts() ) {
                  }

?>

  • Thanks again for all your help. Unfortunately if I us this code my page is blank and giving me errors. I did include the closing ?> – joloshop Feb 3 at 18:57
  • Please let me know which error is showing on your page? – Mohsin Ghouri Feb 3 at 18:58
  • Please also make sure that you have replaced the terms with your term slug in the code. And please remove the closing PHP tag ( ?> ). – Mohsin Ghouri Feb 3 at 18:59
  • Just the header is showing. And I can not add a term slug because it's changing. – joloshop Feb 3 at 19:08
  • Please turn on the debugger by changing the code in wp-config.php. Please see the link => help.one.com/hc/en-us/articles/… for step by step instructions. In this way you can see the error showing on the page. – Mohsin Ghouri Feb 3 at 19:10
0

You can't get the only one post of each taxonomy in a single query. You have to get the single post by using posts_per_page=> 1 for each term and then use the post__in to get the one post of each taxonomy.

  • Thank you, okay new to this can you give me an example? – joloshop Feb 3 at 18:22
  • Code is so long. We have not much space to write the code in the answer of the comment so I have published another answer with the example of the code. please check. – Mohsin Ghouri Feb 3 at 18:41
  • Sorry I was offline. However there is still a misunderstanding. I would like to post 4 posts all together. Within this 4 post there should be only once a post from each APP_TAX_STORE Taxonomy – joloshop Feb 4 at 21:01
  • I have replied you in the chat. please check. – Mohsin Ghouri Feb 5 at 9:24
  • Change it still nothing – joloshop Feb 5 at 18:24

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.