0

I have two main taxonomies: 1. Libros 2. Asuntos

and multiple taxonomy terms: 1. Al Bujari 2. Muslim 3. La Paciencia 4. La Fe 5. La Caridad and many other taxonomy terms using the custom post type: hadices.

The case is that when I click on the slug related to any of the taxonomy terms above, it displays all the posts not just the posts related to the taxonomy term I clicked. I am working on the taxonomy.php file. Here is the code I have on the taxonomy.php:

<?php   $taxes = array( 'libro_de_hadiz' , 'asunto');
    $terms = get_terms( $taxes );
    echo $terms;
    $paged = ( get_query_var( 'paged' ) ) ? absint ( get_query_var( 'paged' ) ) : 1;
        $args = array( 'post_type' => 'hadices', 
                 'posts_per_page' => 6, 
                 'paged' => $paged, 
                  ));
      $loop =  new WP_Query( $args );

    if ( $loop->have_posts() ) : ?>
    <header class="page-header">
        <?php
            the_archive_title( '<h1 class="page-title">', '</h1>' );
            the_archive_description( '<div class="taxonomy-description">', '</div>' );
        ?>
    </header><!-- .page-header -->
<?php endif; ?>

<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">

    <?php
    if ( $loop->have_posts() ) : ?>
        <?php
        /* Start the Loop */
        while ( $loop->have_posts() ) : $loop->the_post();

            /*
             * Include the Post-Format-specific template for the content.
             * If you want to override this in a child theme, then include a file
             * called content-___.php (where ___ is the Post Format name) and that will be used instead.
             */
            get_template_part( 'template-parts/post/content', get_post_format() );

            $loop->the_posts_pagination( array(
            'prev_text' => twentyseventeen_get_svg( array( 'icon' => 'arrow-left' ) ) . '<span class="screen-reader-text">' .$loop-> __( 'Previous page', 'twentyseventeen' ) . '</span>',
            'next_text' => '<span class="screen-reader-text">' . $loop->__( 'Next page', 'twentyseventeen' ) . '</span>' . twentyseventeen_get_svg( array( 'icon' => 'arrow-right' ) ),
            'before_page_number' => '<span class="meta-nav screen-reader-text">' . $loop->__( 'Page', 'twentyseventeen' ) . ' </span>',
        ) );

        endwhile;
        $big = 999999999; // need an unlikely integer
        echo paginate_links( array(
                    'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
                    'format' => '/paged/%#%',
                    'current' => max( 1, get_query_var('paged') ),
                    'total' => $loop->max_num_pages
                ) );
else :

        get_template_part( 'template-parts/post/content', 'none' );

endif; wp_reset_postdata(); ?>




    </main><!-- #main -->
</div><!-- #primary -->
<?php get_sidebar(); ?>

This is the code for my custom post and taxonomies:

function cptui_register_my_cpts() {

/**
 * Post Type: Hadices.
 */

$labels = array(
    "name" => __( "Hadices", "twentyseventeen" ),
    "singular_name" => __( "Hadiz", "twentyseventeen" ),
);

$args = array(
    "label" => __( "Hadices", "twentyseventeen" ),
    "labels" => $labels,
    "description" => "",
    "public" => true,
    "publicly_queryable" => true,
    "show_ui" => true,
    "show_in_rest" => false,
    "rest_base" => "",
    "has_archive" => false,
    "show_in_menu" => true,
    "exclude_from_search" => false,
    "capability_type" => "post",
    "map_meta_cap" => true,
    "hierarchical" => false,
    "rewrite" => array( "slug" => "hadiz", "with_front" => true ),
    "query_var" => true,
    "supports" => array( "title", "editor" ),
    "taxonomies" => array( "libro_de_hadiz", "asunto" ),
);

register_post_type( "hadices", $args );

}

add_action( 'init', 'cptui_register_my_cpts' );

function cptui_register_my_taxes() {

/**
 * Taxonomy: Libros de Hadiz.
 */

$labels = array(
    "name" => __( "Libros de Hadiz", "twentyseventeen" ),
    "singular_name" => __( "Libro", "twentyseventeen" ),
);

$args = array(
    "label" => __( "Libros de Hadiz", "twentyseventeen" ),
    "labels" => $labels,
    "public" => true,
    "hierarchical" => false,
    "label" => "Libros de Hadiz",
    "show_ui" => true,
    "show_in_menu" => true,
    "show_in_nav_menus" => true,
    "query_var" => true,
    "rewrite" => array( 'slug' => 'libro_de_hadiz', 'with_front' => true, ),
    "show_admin_column" => true,
    "show_in_rest" => false,
    "rest_base" => "",
    "show_in_quick_edit" => false,
);
register_taxonomy( "libro_de_hadiz", array( "hadices" ), $args );
register_taxonomy( "asunto", array( "hadices" ), array(
    "name" => __( "Asuntos", "twentyseventeen" ),
    "singular_name" => __( "Asunto", "twentyseventeen" ),
    "label" => __( "Asuntos", "twentyseventeen" ),
    "public" => true,
    "hierarchical" => false,
    "label" => "Asuntos",
    "show_ui" => true,
    "show_in_menu" => true,
    "show_in_nav_menus" => true,
    "query_var" => true,
    "rewrite" => array( 'slug' => 'asunto', 'with_front' => true, ),
    "show_admin_column" => true,
    "show_in_rest" => false,
    "rest_base" => "",
    "show_in_quick_edit" => false,
) );
}

add_action( 'init', 'cptui_register_my_taxes' );

I have seen the answers to the post in this link: Archive template for taxonomy terms

However the answers were just saying to add a taxonomy-term.php for each term, which will be very tedious in my case as I would need tens of files with similar code just changing the term slug in the tax_query. Is there a way to add a dynamic variable for the terms in tax_query to show only the posts related to the term or any other solution if any?

Thanks in advance

7
  • Is there a reason you’re calling your own query rather than using the default query? Apr 7, 2018 at 10:26
  • Yes as I am calling a custom post type and custom taxonomies.
    – user141312
    Apr 7, 2018 at 10:46
  • But WordPress already does this for you. Apr 7, 2018 at 10:53
  • How is it doing so? Excuse me as I am a beginner.
    – user141312
    Apr 7, 2018 at 10:59
  • I’m on my phone so can’t answer easily. I’ll look when I’m home if no-one else has answered. Apr 7, 2018 at 11:07

1 Answer 1

0

As Andy said, WordPress will load the correct posts for you. You don't need a custom query. When you're viewing a custom post type or taxonomy archive the main loop will already have the correct posts based on the URL.

So you don't need any of this:

$taxes = array( 'libro_de_hadiz' , 'asunto');
    $terms = get_terms( $taxes );
    echo $terms;
    $paged = ( get_query_var( 'paged' ) ) ? absint ( get_query_var( 'paged' ) ) : 1;
        $args = array( 'post_type' => 'hadices', 
                 'posts_per_page' => 6, 
                 'paged' => $paged, 
                  ));
      $loop =  new WP_Query( $args );

Then $loop->have_posts() and $loop->the_post() should just be have_posts() and the_post().

Also, you got a bit carried away and added $loop-> where it doesn't make sense. You should remove it from everywhere else too.

5
  • That's true, it worked. What confused me was the articles and the tutorials on WP_Query but it seems they are fit more for archive.php or for a custom page not for the main taxonomy.php. Thank you so much.
    – user141312
    Apr 7, 2018 at 13:28
  • archive.php also works this way. WP_Query is only if you want to query posts separate to the main query, for a widget or some-such. Apr 7, 2018 at 13:30
  • Just I had another question, if I want to give the option for the website visitor to query more than a taxonomy term so as to display the related post types in a separate page, shall it be through a form in a search.php file or it has another way to do it?
    – user141312
    Apr 7, 2018 at 13:32
  • I'm not sure I fully understand the question. If you want to have a page for all Hadices then that will already exist at /hadices, as long as public is true in register_post_type(). That page will use whichever template is available based on the Template Hierarchy. Apr 7, 2018 at 13:38
  • Actually I meant something else. What I meant is that I want something like a form or a query bar where the site visitor can see the hadices based on the taxonomies he chooses. So for example the user wants to see the hadices related to the taxonomy term Al Bujari and the taxonomy term La Paciencia, he makes a query that directs him to a page showing the hadices tagged with Al Bujari and La Paciencia. Thank you so much for your help and your note on WP_Query.
    – user141312
    Apr 7, 2018 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.