I’m developing a website for a Golfing Society. They want to be able to define and list their fixtures for 2014. But my shortcode is not producing results.
Using my new oik-types plugin I defined the new custom post type
fixture ( labels “Fixtures” and “Fixture” ). I registered a custom category
format that indicates the type of competition, e.g. foursomes, 4BBB (four ball better ball). Custom fields for each fixture include
On the home page I wanted to list the upcoming fixtures displaying the date and event title acting as a link to the details. So using my [bw_pages] shortcode I dutifully wrote the shortcode parameters
But nothing appeared.
I reduced the shortcode to
bw_pages post_type=fixture and that worked.
Then I put the
format parameter back in and it failed.
Then I started problem determination…
- Can you reproduce it locally? Yes.
- Are you running the same versions? Yes. My code is the same, but the problem’s occurring on WordPress 3.7.1.
- Have you got trace output? Yes. get_posts() isn’t returning anything
- If you change the code does it work? Yes. If I don’t pass the
formatparameter to get_posts()
- Have you grepped the code for
format. Yes. But there’s nothing obvious.
- If you enable action tracing can you find out why no data is returned?
and then it dawned on me.
The debug trace of query vars showed
format being used as a custom category search,
_T as the category value…
AND since I’d seen something like this before, I knew that was the problem.
Is there a fix?
Yes. Change the custom category name to “fixture_format”.
With hindsight I should have followed the Top 10 questions to ask when something doesn’t work. In some respects I did, but it took me a lot longer to find the answer than if I’d consulted the stupid question asker who would have asked
Why didn’t you call your new taxonomy ‘competition_format’ or ‘fixture_format’?
Who was doing it wrong?
First I thought it was me… passing shortcode parameters to get_posts(). Then I thought it was WordPress itself … but that’s how query vars is intended to work. So it turns out it was the user – defining a custom category name that’s also being used in code. How was he supposed to know that was wrong?
The moral of the story is:
Name space everything.
- So that the custom fields don’t get shown in the Custom Fields meta box their names are prefixed with an underscore ( _ ). This is belt and braces; we could simply have de-selected supports ‘custom-fields’ for the fixture post type.
- The fact that the problem occurred with the latest version of WordPress was a red-herring. I have not only reproduced the problem with previous version but also with passing the same parameter to a completely different shortcode.
- A previous problem with get_posts() was when I realised I had multiple uses for the
id=parameter. In some cases it was being used to set the HTML id= attribute in others to choose the post(s) to display.