I want to use the page slug as the taxonomy when calling a list of custom posts.

This works:

<?php $page_slug = basename(get_permalink()); ?>
<?php echo  $page_slug; ?>

It shows "december-2017" on the page.

So does this:

<ul>
    <?php 
        $args = array(
            'post_type' => 'nomination',
            'post_status' => 'draft',
            'tax_query' => array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'month_category',
                    'field' => 'slug',
                    'terms' => 'december-2017',
                )
            )
        );
        $the_query = new WP_Query( $args ); 
    ?>
    <?php if ( $the_query->have_posts() ) : ?>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <li>
        <?php the_terms( $post ->ID, 'dealership_category', '', '', '' ); ?><br>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br> 
        Submitted: <?php echo get_the_date( 'd/m/Y' ); ?>
    </li>
    <?php endwhile; ?>
    <?php wp_reset_postdata(); ?>
    <?php else : ?>
    <li>Nothing found.</li>
    <?php endif; ?>
    </ul>

So why can't I get this to work:

<ul>
<?php $page_slug = basename(get_permalink()); ?>

    <?php
        $args = array(
            'post_type' => 'nomination',
            'post_status' => 'draft',
            'tax_query' => array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'month_category',
                    'field' => 'slug',
                    'terms' => '$page_slug',
                )
            )
        );
        $the_query = new WP_Query( $args ); 
    ?>
    <?php if ( $the_query->have_posts() ) : ?>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <li>
        <?php the_terms( $post ->ID, 'dealership_category', '', '', '' ); ?><br>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br> 
        Submitted: <?php echo get_the_date( 'd/m/Y' ); ?>
    </li>
    <?php endwhile; ?>
    <?php wp_reset_postdata(); ?>
    <?php else : ?>
    <li>Test not working.3</li>
    <?php endif; ?>
    </ul>

It shows "Test not working.3" on the page. I'm not great at PHP so am confused.

up vote 2 down vote accepted

In your tax query, you're searching for a string instead of a variable, the only time it will match currently is if the term is literally "$page_slug". You should remove the single quotes around the $page_slug to have it parsed as a variable, so:

'terms' => '$page_slug'

should be:

 'terms' => $page_slug,
  • Thank you for commenting, I really appreciate your explanation as it's helping me learn. I did originally try it without the quote marks but it disn't work. I just tried it again and it works! – user6744245 Feb 28 at 9:41

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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