简体   繁体   中英

Loop specific category of a custom post type in wordpress

Just fixed it.... the problem was in

    register_taxonomy_for_object_type( 'tags', 'produto' );

was registering tags instead of categories.... fixed with:

<?php
                                                $tag = 'taeq';
                                                $args = array('post_type' => 'produto', 'posts_per_page' => -1, 'produto_category' => $tag);
                                                $loop = new WP_Query($args);
                                                while ($loop->have_posts()) : $loop->the_post(); ?>
                                                    <li>
                                                        <img src="<?php the_field('produto_img'); ?>" alt="<?php the_title(); ?>" />
                                                        <span><?php the_title(); ?></span>
                                                        <span><?php the_field("produto_desc"); ?></span>
                                                        <i class="border"></i>
                                                    </li>
                                                <?php endwhile; ?>

The correct question was how to Loop specific tag of a custom post type in wordpress

I'm trying to loop posts from only one category on wordpress.

I don't know nothing about PHP...

Here is the code I have, working, but displaying all the products

<?php 
$new_query = new WP_Query('post_type=produto&post_per_page=-1');
while($new_query -> have_posts()) : $new_query -> the_post();
?>
<li>
    <img src="<?php the_field("produto_img"); ?>" alt="<?php the_title(); ?>" />
    <span><?php the_title(); ?></span>
    <span><?php the_field("produto_desc"); ?></span>
    <i class="border"></i>
</li>

<?php endwhile; ?>

I need show items from category ID 2.

what should I do?

OBS: My site is a singlepage website. I'm displaying all the posts types in differents places of the same page. need filter some by category.

functions php:

add_action( 'init', 'create_post_type_produto' );
function create_post_type_produto() {
    $labels = array(
        'name' => _x('Produtos', 'post type general name'),
        'singular_name' => _x('Produtos', 'post type singular name'),
        'add_new' => _x('Adicionar novo', 'produto'),
        'add_new_item' => __('Adicionar novo produto'),
        'edit_item' => __('Editar produto'),
        'new_item' => __('Novo produto'),
        'all_items' => __('Todos os produtos'),
        'view_item' => __('Ver produtos'),
        'search_items' => __('Procurar produtos'),
        'not_found' =>  __('Nenhum produto encontrado'),
        'not_found_in_trash' => __('Nenhum produto encontrado na lixeira.'),
        'parent_item_colon' => '',
        'menu_name' => 'Produtos'
    );
    register_post_type( 'produto', array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'has_archive' => 'produtos',
        'rewrite' => array(
         'slug' => 'produtos',
         'with_front' => false,
        ),
        'capability_type' => 'post',
        'has_archive' => true,
        'hierarchical' => false,
        'menu_position' => null,
        'supports' => array('title')
        )
    );
    register_taxonomy( 'produto_category', array( 'produto' ), array(
        'hierarchical' => true,
        'label' => __( 'Categoria do produto' ),
        'labels' => array( // Labels customizadas
        'name' => _x( 'Categorias', 'taxonomy general name' ),
        'singular_name' => _x( 'Categoria', 'taxonomy singular name' ),
        'search_items' =>  __( 'Procurar categorias' ),
        'all_items' => __( 'Todas categorias' ),
        'parent_item' => __( 'Categoria pai' ),
        'parent_item_colon' => __( 'Categoria pai:' ),
        'edit_item' => __( 'Editar categoria' ),
        'update_item' => __( 'Atualizar categoria' ),
        'add_new_item' => __( 'Adicionar nova categoria' ),
        'new_item_name' => __( 'Nome da nova categoria' ),
        'menu_name' => __( 'Categoria' ),
        ),
        'show_ui' => true,
        'show_in_tag_cloud' => true,
        'query_var' => true,
        'rewrite' => array(
            'slug' => 'produtos/categorias',
            'with_front' => false,
        ),)
    );
    register_taxonomy_for_object_type( 'tags', 'produto' );
}

Try this using tax query for custom taxonomies filter in wp query

 // using category slug
 $args = array(  
          'post_type' => 'produto', 
          'posts_per_page' => -1, 
          'tax_query' => array(
            array(
                'taxonomy' => 'produto_category',
                'field'    => 'slug', // term_id, slug  
                'terms'    => 'taeq',
            ),
           )
         );

// using category id
/* $args = array(  
          'post_type' => 'produto', 
          'posts_per_page' => -1, 
          'tax_query' => array(
            array(
                'taxonomy' => 'produto_category',
                'field'    => 'term_id', // term_id, slug  
                'terms'    => 5,
            ),
           )
         );

*/ 

$loop = new WP_Query($args);

Wp query more tax reference https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

You can use the category parameter in the WP_Query. So you can change your WP_Query to:

WP_Query('post_type=produto&post_per_page=-1&cat=4');

Where cat=4 is the category id.

You can see other ways to define it here http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

By using the cat (or product_cat for products) element (also use $args array for clarity) example:

$cat = 2; // The product category you want to display
$args = array('post_type' => 'produto', 'posts_per_page' => -1, 'cat' => $cat);
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post(); ?>
    <li>
        <img src="<?php the_field('produto_img'); ?>" alt="<?php the_title(); ?>" />
        <span><?php the_title(); ?></span>
        <span><?php the_field("produto_desc"); ?></span>
        <i class="border"></i>
    </li>
<?php endwhile; ?>

i use custom taxonomy filter like this

Event is my custom post type and featured-events is event category slug...

it works perfect for me, hope it helps :)

 $loop = new WP_Query( array( 
                         'post_type' => 'event','tax_query' => array(
                                                           array(
                                                           'taxonomy' => 'event-categories',
                                                           'field'    => 'slug',
                                                           'terms'    => 'featured-events',
                                                           )
                                                     )) );

You are using custom taxonomy so you cannot use the argument for category, you should use Taxonomy querying https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters So your code should be

<?php 
$args = array(  
    'post_type' => 'produto', 
    'posts_per_page' => -1, 
    'tax_query' => array(
        array(
            'taxonomy' => 'produto_category',
            'field'    => 'slug', // search by slug name, you may change to use ID
            'terms'    => 'taeq', // value of the slug for taxonomy, in term using ID, you should using integer type casting (int) $value
        ),
    )
);
$new_query = new WP_Query($args);
while($new_query -> have_posts()) : $new_query -> the_post();
?>
<li>
    <img src="<?php the_field("produto_img"); ?>" alt="<?php the_title(); ?>" />
    <span><?php the_title(); ?></span>
    <span><?php the_field("produto_desc"); ?></span>
    <i class="border"></i>
</li>

<?php endwhile; ?>

if you are trying to show only one category on category template u can use

this code

query_posts('cat=6');// while 6 is category id you want to show

before this code in your wp template

if ( have_posts() ) :
                    while ( have_posts() ) : the_post();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM