Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

slug not always accessible for queries within author taxonomy #107

Closed
charlesrsimmons opened this Issue Feb 15, 2013 · 3 comments

Comments

Projects
None yet
2 participants

(This is my first ever anything on github, so I may be doing this wrong, but wanted to contribute)

While building a real estate site I had an issue with tax_query not working for only two out of 44 authors. Obviously, all are set up the same, the database fields for all terms are identical, but no query I ran could find the slug for just two authors; var_dump, etc... were all empty, false or null.

Original tax_query that worked for all but two authors:

$args = array(
               'tax_query' => array(
                                       array(
                                          'taxonomy' => 'author',
                                          'field' => 'slug',
                                          'terms' => $curauth->ID
                                       )
                                     )
             );

The only way to get this query working was to specify the term_id for those two authors (couldn't do it dynamically because I couldn't access the slug for those two authors using any kind of query) using the below modified example tax_query:

if(is_author('author-slug')) { $agent_term_id = '1'; } else { $term = get_term_by('slug', $curauth->user_login, 'author'); $agent_term_id = $term->term_id; }
$args = array(
               'tax_query' => array(
                                       array(
                                          'taxonomy' => 'author',
                                          'field' => 'term_id',
                                          'terms' => array($agent_term_id)
                                       )
                                     )
             );

This format ended up working for all co-authors, but is obviously not ideal because you'd have to go and get the term_id from the database and manually enter it in the query.

Collaborator

danielbachhuber commented Feb 18, 2013

Hey @chazbeaner, the WordPress.org support forum is a good place for questions like this one.

You can probably get your query working by using 'slug' as the field argument. Then, in terms you can pass an array of prefixed user logins:

$args = array(
               'tax_query' => array(
                                       array(
                                          'taxonomy' => 'author',
                                          'field' => 'slug',
                                          'terms' => array( 'cap-danielbachhuber', 'cap-chazbeaner' )
                                       )
                                     )
             );

Hope this helps.

Hey Daniel - Thanks for the response. I did try the support forums, but didn't get an answer. So, I did a lot of troubleshooting myself, which is when I brought it to github because I thought I was identifying a potential issue/bug.

The solution you provide below does work for the two authors with whom I'm having the issue, but fails for all other authors.

$args = array(
               'tax_query' => array(
                                       array(
                                          'taxonomy' => 'author',
                                          'field' => 'slug',
                                          'terms' => array('cap-'.$curauth->user_login)
                                       )
                                     )
             );

Even if I manually type in the other authors to this query, with the prefix, they all fail except for the two problem children. I could specify the prefix manually for just those two authors and leave the rest with their regular slugs, but that's really not all that much better than the term_id solution and still leaves me without a dynamic query.

Sorry for submitting in the wrong place. I'll keep banging my head against it and see if I can figure out why it's not working.

Collaborator

danielbachhuber commented Feb 19, 2013

fails for all other authors.

If you hit "Update" on a post the author is assigned to, I think it will fix the problem. Their terms probably don't yet have the 'cap-' prefix, and updating will automagically assign it.

Alternatively, you can directly edit the term slugs in the terms database to include the prefix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment