0

I have created a custom portfolio with the following code in the function.php in a custom template.

if ( ! function_exists('custom_post_type_portafolio') ) {

// Register Custom Post Type
 function custom_post_type_portafolio() {

$labels = array(
 'name' => _x( 'Proyectos', 'Post Type General Name', 'text_domain' ),
 'singular_name' => _x( 'Proyecto', 'Post Type Singular Name', 'text_domain' ),
 'menu_name' => __( 'Portafolio', 'text_domain' ),
 'name_admin_bar' => __( 'Portafolio', 'text_domain' ),
 'archives' => __( 'Portafolio', 'text_domain' ),
 'parent_item_colon' => __( 'Proyecto superior', 'text_domain' ),
 'all_items' => __( 'Todos los proyectos', 'text_domain' ),
 'add_new_item' => __( 'Añadir nuevo proyecto', 'text_domain' ),
 'add_new' => __( 'Añadir nuevo Proyecto', 'text_domain' ),
 'new_item' => __( 'Nuevo proyecto', 'text_domain' ),
 'edit_item' => __( 'Editar proyecto', 'text_domain' ),
 'update_item' => __( 'Actualizar proyecto', 'text_domain' ),
 'view_item' => __( 'Ver proyecto', 'text_domain' ),
 'search_items' => __( 'Buscar proyecto', 'text_domain' ),
 'not_found' => __( 'Not found', 'text_domain' ),
 'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ),
 'featured_image' => __( 'Imagen destacada', 'text_domain' ),
 'set_featured_image' => __( 'Añadir imagen destacada', 'text_domain' ),
 'remove_featured_image' => __( 'Quitar Imagen destacada', 'text_domain' ),
 'use_featured_image' => __( 'Usar como Imagen destacada', 'text_domain' ),
 'insert_into_item' => __( 'Insert into item', 'text_domain' ),
 'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ),
 'items_list' => __( 'Items list', 'text_domain' ),
 'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
 'filter_items_list' => __( 'Filter items list', 'text_domain' ),
 );
 $args = array(
 'label' => __( 'Proyecto', 'text_domain' ),
 'description' => __( 'Portafolio', 'text_domain' ),
 'labels' => $labels,
 'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', 'post-formats', ),
 'taxonomies' => array( 'Portafolio-category', 'post_tag' ),
 'hierarchical' => true,
 'public' => true,
 'show_ui' => true,
 'show_in_menu' => true,
 'menu_position' => 5,
 'show_in_admin_bar' => true,
 'show_in_nav_menus' => true,
 'can_export' => true,
 'has_archive' => true,
 'exclude_from_search' => false,
 'publicly_queryable' => true,
 'capability_type' => 'page',
 );
 register_post_type( 'portafolio', $args );

}
 add_action( 'init', 'custom_post_type_portafolio', 0 );

}


if ( ! function_exists( 'custom_taxonomy_portafolio' ) ) {

// Register Custom Taxonomy
function custom_taxonomy_portafolio() {

    $labels = array(
        'name'                       => _x( 'Tipos de proyectos', 'Taxonomy General Name', 'text_domain' ),
        'singular_name'              => _x( 'Tipo Proyecto', 'Taxonomy Singular Name', 'text_domain' ),
        'menu_name'                  => __( 'Tipos de proyectos', 'text_domain' ),
        'all_items'                  => __( 'Todos los tipos de proyectos', 'text_domain' ),
        'parent_item'                => __( 'Tipo superior', 'text_domain' ),
        'parent_item_colon'          => __( 'Tipo superior:', 'text_domain' ),
        'new_item_name'              => __( 'Nuevo Tipo de proyecto', 'text_domain' ),
        'add_new_item'               => __( 'Añadir Nuevo Tipo de proyecto', 'text_domain' ),
        'edit_item'                  => __( 'Editar Tipo de proyecto', 'text_domain' ),
        'update_item'                => __( 'Actualizar Tipo de proyecto', 'text_domain' ),
        'view_item'                  => __( 'Ver Tipo de proyecto', 'text_domain' ),
        'separate_items_with_commas' => __( 'Separar tipos con comas', 'text_domain' ),
        'add_or_remove_items'        => __( 'Añadir o quitar Nuevo Tipos de proyecto', 'text_domain' ),
        'choose_from_most_used'      => __( 'Elegir entre los más usados', 'text_domain' ),
        'popular_items'              => __( 'Tipos de proyecto populares', 'text_domain' ),
        'search_items'               => __( 'Buscar Tipos de proyecto', 'text_domain' ),
        'not_found'                  => __( 'No se encuentra', 'text_domain' ),
        'no_terms'                   => __( 'No hay Tipos de proyecto', 'text_domain' ),
        'items_list'                 => __( 'Lista de Tipos de proyecto', 'text_domain' ),
        'items_list_navigation'      => __( 'Navegación Tipos de proyecto', 'text_domain' ),
    );
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => true,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
    );
    register_taxonomy( 'tipo_proyecto', array( 'portafolio' ), $args );

}
add_action( 'init', 'custom_taxonomy_portafolio', 0 );

}

Print the posts correctly, do the loop of the categories for the menu well, on my portfolio.php page of my theme. but I can not filter the projects by categories with the following code in archives.php.

 <div class="ai_section_2_2_proyectos">
                        <ul class="list-unstyled listado_proyectos">
                            <?php
                            $args=array(
                                'post_type' => 'Portafolio',
                                'post_status' => 'publish',
                                'orderby' => 'ASC',
                                'posts_per_page' => '-1',
                                'taxonomies' => array( 'tipo_proyecto', 'ecommerce' ),
                            );
                            // The Query
                            $the_query = new WP_Query( $args );
                            // The Loop
                            if ( $the_query->have_posts() ) {
                                while ( $the_query->have_posts() ) {
                                    $the_query->the_post();
                                    ?>
                                    <li id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                                        <a href="<?php the_permalink();?>" title="<?php the_title();?>" class="a_hover" data-image="<?=get_the_post_thumbnail_url($post->ID, 'full')?>">
                                            <h2><?php the_title();?></h2>
                                            <ul class="list-unstyled list-inline post-categories">
                                                <?
                                                $category = get_the_category();
                                                foreach( $category as $cat )
                                                {
                                                    echo "<li class='categories_li'>".$cat->name."</li>";
                                                    if ($cat === end($category)) {
                                                    }
                                                    else{
                                                        echo "<li class='categories_li'>/</li>";
                                                    }
                                                }
                                                ?>
                                                <li class="ver_proyecto">Ver proyecto</li>
                                            </ul>
                                        </a>
                                    </li>
                                <?php }} else {
                                echo 'No hay artículos';
                                // no se encontraron artículos
                            }
                            /* Restore original Post Data */
                            wp_reset_postdata();
                            ?>
                        </ul>
                    </div>

I think the problem is in this part:

                    $args=array(
                        'post_type' => 'Portafolio',
                        'post_status' => 'publish',
                        'orderby' => 'ASC',
                        'posts_per_page' => '-1',
                        'taxonomies' => $cat,
                    );
                    // The Query
                    $the_query = new WP_Query( $args );
  • Hello, not sure it change something, but your post type is "portafolio" & you query "Portafolio" with a CAPS on the P. – Gregory Mar 7 at 23:09
0

I don't think that the taxonomies param is correct.

To use taxonomies in WP_Query args, they need to be wrapped in an array, and passed in via the tax_query param

https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

0

But this code works in the categories filter of the post:

$args=array(
                                    'post_type' => 'post',
                                    'post_status' => 'publish',
                                    'orderby' => 'ASC',
                                    'posts_per_page' => '-1',
                                    'category__in' => $cat,
                                );
                                // The Query
                                $the_query = new WP_Query( $args );

Why does not this work with the portfolios?

I have worked with this other solution but it does not work.

                    $args=array(
            'post_type' => 'portafolio',
            'tax_query' => array(
                                    array(
                                        'taxonomy' => 'tipo_proyecto',
                                        'field'    => 'slug',
                                        'terms'    => $cat,
                                    ),
                                ),
                    );
                    // The Query
                    $the_query = new WP_Query( $args );

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.