Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Can't get WordPress custom post type posts by custom taxonomy

  • This topic is empty.
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #208365
    rolandas
    Participant

    I got this code below:

    <?php
    $type = 'products';
    $args=array(
      'post_type' => $type,
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
    

    It show all posts, but I need posts from certain category only. So, if I add:

    'category_name' => $terms_as_text
    

    ($terms_as_text is a current category)- I get no posts at all :(

    Just to mention- I’m working on a single page (single-products.php)

    Thanks :)

    #208407
    Alen
    Participant

    @rolandas What is $terms_as_text? Where is it coming from? Does the category exist? Also, caller_get_posts was replaced by ignore_sticky_posts.


    $args = array( 'post_type' => array('products'), 'category_name' => 'CATNAME', 'ignore_sticky_posts' => true, ); $query = new WP_Query( $args ); if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); ?> <!-- Do Magic Here --> <?php the_title(); ?> <?php } } else { echo 'No Posts Found!'; } wp_reset_postdata();

    You might also try re-saving (resetting) Permalinks. On the Permalink Settings screen just select default, hit save, then change it back to post name, hit save… that’s it.

    Hope that helps, Alen.

    #208687
    rolandas
    Participant

    Hi @AlenAbdula. Thanks for your reply.

    I tried your code, but I get “No Posts Found!”.

    I forget to mention earlier that I’m using WordPress plug in “Types” (https://wp-types.com/home/types-manage-post-types-taxonomy-and-custom-fields/).

    #208692
    Alen
    Participant

    Not sure what else. Did you create any products and mark them under category you’re trying to query? Can you copy your index.php file and name it archive-products.php and see if visiting /products works.

    #209638
    Dragore
    Participant

    Heres the a snippet that I use, hopefully it helps.

    <?php $recent = new WP_Query(array('cat' = 'CAT_ID')); while($recent->have_posts()) : $recent->the_post(); ?>
    ...
    <?php endwhile; ?>
    
    

    Then I use the snippet

    <?php echo types_render_field('TYPES_FIELD_SLUG'); ?>
    
    

    to get the types custom field.

    #209680
    tommi20
    Participant

    so its types_render_file ? ill try it


    Es Krim

    #209849
    rolandas
    Participant

    @Dragore, @Alen thanks for replies.
    I found how to do it. In case anyone needs help:

      $args = array (
      'posts_per_page'            =>  -1,
      'post_type'              => array( 'my-custom-post-type' ),
      'order'                  => 'ASC',
      'orderby'                => 'title',
      'cache_results'          => true,
      'update_post_meta_cache' => true,
      'update_post_term_cache' => true,
      'tax_query' => array(
        array(
          'taxonomy' => 'my-custom-post-type-category',
          'field' => 'slug',
          'terms' => array($current_post_ category)
         )
       )
    );
    
Viewing 7 posts - 1 through 7 (of 7 total)
  • The forum ‘Back End’ is closed to new topics and replies.