最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

List custom post type by custom tax term as page title

matteradmin9PV0评论

I have a page called "Developments" and I want to list all the Project posts by the custom tax Project Type using the page title as the term.

This doesn't work:

<?php

$title_slug = sanitize_title(wp_title($sep = ''));

$the_query = new WP_Query( array(
    'post_type' => 'project',
    'tax_query' => array(
        array (
            'taxonomy' => 'project_type',
            'field' => 'slug',
            'terms' => $title_slug,
        )
    ),
) );

while ( $the_query->have_posts() ) :
    $the_query->the_post();
    get_template_part( 'template-parts/content', 'post' );
endwhile;
wp_reset_postdata();

?>

It doesn't work with 'terms' => $title either (hence I tried to define a new variable $title_slug).

If I use 'terms' => 'Developments' it works.

Side question: why does $title_slug = sanitize_title(wp_title($sep = '')); print out the title to display? I am trying to define a variable not echo anything. Answered below

EDIT:

Changing to this format works:

...
$options = array(
    'post_type' => 'project',
    'tax_query' => array(
      array (
          'taxonomy' => 'project_theme',
          'field' => 'slug',
          'terms' => $title_slug,
      )
    ),
);

$the_query = new WP_Query( $options );
...

But I still don't understand why the original is failing to list posts.

Post a comment

comment list (0)

  1. No comments so far