0

I am using get_the_title to set up the taxonomy term for my query, which has worked fine in testing up until I had a title with more than one word, title of "catalogs" works, title of "new catalogs" does not work. When I say doesn't work I mean that, the_post_thumbnail and get_post_meta don't return anything.

<?php
    $supplier = get_the_title();

    global $post;
    $args = array(
        'numberposts' => 1,
        'post_type' => 'suppliers',
        'taxonomy' => 'suppliersTax',
        'term' => $supplier,
        'post_status' => 'publish'
    );
    wp_list_categories( $args );
    $homeSOTMHeader = get_posts( $args );
    foreach( $homeSOTMHeader as $post ) :   setup_postdata($post);
?>

    <div id="supplierHeader">
        <div id="supplierLogo" class="alignLeft">
            <?php if ( has_post_thumbnail() ) {the_post_thumbnail();} ?>
        </div>

        <ul id="companyHeaderInfoHorizontal">
            <?php
                $meta = get_post_meta(get_the_ID(), 'sp_homeHeaderCompanyLink', true); 
                '' != $meta and print "<li><a href='$meta' target='_blank'>Company Site</a></li>";

                $meta = get_post_meta(get_the_ID(), 'sp_homeHeaderProductsLink', true); 
                '' != $meta and print "<li><a href='$meta' target='_blank'>Browse Products</a></li>";

                $meta = get_post_meta(get_the_ID(), 'sp_homeHeaderContactsBox', true);
                '0' != $meta and print "<li><a class='inline' href='#contactsModal'>Contacts</a></li>";
            ?>
        </ul>

        <?php endforeach; ?>
        <?php // Reset Post Data
        wp_reset_postdata(); ?>

    </div> <!-- supplierHeader -->
1
  • What kind of template is this code in? Is it a page template? A taxonomy archive?
    – Tom J Nowell
    Apr 10, 2012 at 15:51

1 Answer 1

1

The term needs the slug of the term, not the human read description so "hello-world" not "Hello World"

Try using the sanitize_title function: http://codex.wordpress.org/Function_Reference/sanitize_title

Also, add an else case for your loop to catch the 'no posts found' case, etc, when you have an issue it's a good idea to have as much information as possible.

Additionally:

    'taxonomy' => 'suppliersTax',
    'term' => $supplier,

can also be written as:

    'suppliersTax' => $supplier,

edit: the most verbose definitive precise flexible method of writing it is this:

'tax_query' => array(
    array(
        'taxonomy' => 'supplier',
        'field' => 'slug',
        'terms' => sanitize_title($supplier)
    )
)

as seen here: http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

Also looking at your code in your comment you use:

$homeSOTMHeader = get_posts( $args );

foreach( $homeSOTMHeader as $post ) :   setup_postdata($post);
?>
    // stuff
<?php endforeach;?>
<?php wp_reset_postdata(); ?>

It would be clearer to use this instead:

$q = new WP_Query($args);
if($q->have_posts()){
    while($q->have_posts()){
        $q->the_post();
        // stuff
    }
} else {
    ?><p>No Posts were found</p><?php // important information to have for debugging purposes
}
wp_reset_postdata();
5
  • That makes sense, I did what you said, the ones that were working before still work, and the ones that didn't still don't, I've created a pastebin to show what I've done, pastebin.com/hAhXgh4k
    – Chuck
    Apr 10, 2012 at 13:34
  • Your original syntax is not recommended, I've edited my answer, please use the syntax referred to by the WordPress codex instead, and adjust your post loop to catch the 'no posts found' situation and report back what happens
    – Tom J Nowell
    Apr 10, 2012 at 14:29
  • I've updated as you suggested, the ones that were working before still work, the ones that weren't working before now just say "No Posts were found"
    – Chuck
    Apr 10, 2012 at 15:42
  • This is good! We now have more information to work with which is always a good thing. Now, can you show me an example where the sanitised title is not right, and what the correct value should be so that I can see what the difference between the sanitised title and the correct term slug is?
    – Tom J Nowell
    Apr 10, 2012 at 15:49
  • 1
    Wow, nevermind, it was just coincidence that the two that had titles with more than one word, someone had unchecked their taxonomies. Thanks for the other tips though, the "No Posts were found" is what made me go back and check the most obvious thing.
    – Chuck
    Apr 10, 2012 at 15:52

Your Answer

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

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