- This topic is empty.
-
AuthorPosts
-
September 19, 2015 at 3:19 am #208365rolandasParticipant
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 :)
September 20, 2015 at 4:19 am #208407AlenParticipant@rolandas What is
$terms_as_text
? Where is it coming from? Does the category exist? Also,caller_get_posts
was replaced byignore_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.
September 25, 2015 at 7:50 am #208687rolandasParticipantHi @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/).
September 25, 2015 at 11:15 am #208692AlenParticipantNot 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 itarchive-products.php
and see if visiting/products
works.October 13, 2015 at 12:10 pm #209638DragoreParticipantHeres 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.
October 14, 2015 at 7:31 am #209680October 18, 2015 at 3:58 am #209849rolandasParticipant@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) ) ) );
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.