0

I've spent all day trying to work out how to do this, and have only advanced a little - and that's mainly in finding methods that DON'T work. Am hoping someone knows something or can see something that I'm missing!

I have two custom post types: Episode and Snippets. Episode holds some info itself but also the display then builds itself using Snippets. I achieve this by both the Episode and all of the Snippets having the same title. So when I view an Episode part of the process is that it looks at all Snippets and displays those which have the same title. Each associated snippet has the same slug as the Episode plus a unique identifying suffix - e.g. '-1', '-2', '-3', etc.

I now need to query the database to find out if an Episode has a particular type of Snippet associated with it. This SHOULD be easy if 'page_title' was a valid entry in $args but it seems that it isn't. (It took me a sizeable chunk of today to realise that!)

The first six characters of the Snippet slug are the same as the Episode slug. But doesn't this mean I need to use a wildcard on the left-hand side of the $arg line? In words, "The first six characters of the slug => Episode_slug".

Can anyone suggest how I can achieve this?

(I don't think it will be particularly helpful but you can see an example of how the two post types work together here.)

  • 1
    why not instead come up with a more sane way to relate snippets to episodes? like store the episode ID in a snippet custom field. – Milo Jun 8 '15 at 17:03
  • 1
    or use a custom (non-hierarchical?) taxonomy to link them together? – birgire Jun 8 '15 at 17:06
  • @milo Kneejerk reaction was '... because ...' but the more I think about it, the more that idea has merit. I've got oodles of snippets for one show but in the long term it would address other issues like how long it can take to compile a page. I've got another show without many snippets yet which works the same way, so perhaps I can implement a solution using your suggestion to check it does what I need and is easy to use, then apply it to the other if successful. Thanks for making me look at it differently! :-) – Kevin4fm Jun 8 '15 at 17:38
  • @birgire Sometimes things that strike one person as immediately obvious bypass another entirely. Thanks for making me rethink! :-) – Kevin4fm Jun 8 '15 at 17:40
0

I agree with the comments to the question that there is probably a better way to associate your post types, however, the question is interesting in a "code golf" sort of way :)

I now need to query the database to find out if an Episode has a particular type of Snippet associated with it. This SHOULD be easy if 'page_title' was a valid entry in $args but it seems that it isn't. (It took me a sizeable chunk of today to realise that!)

name and pagename both address the slug but not with a wildcard, as you can see if you try this:

$args = array(
  'post_type' => 'book',
  'name' => 'test',
);
$q = new WP_Query($args);
var_dump($q->request);

The easiest way to accomplish that wildcard trick is with a filter on posts_where:

function wildcard_post_slug($where) {
  remove_filter('posts_where','wildcard_post_slug');
  global $wpdb;
  return $where.' AND '.$wpdb->posts.'.post_name LIKE "Episode_%"';
}
add_filter('posts_where','wildcard_post_slug');
$args = array(
  'post_type' => 'book',
);
$q = new WP_Query($args);
var_dump($q->request);
| improve this answer | |
  • Thanks for these. The first has already moved me on further than I've got all day! Brilliant. So easy ... when you know how! Struggling a little to get at the results of the query, but I'm pretty sure they're there. The var_dump shows the query itself is spot on. I think I'm now suffering from thinking about what the others suggested. It makes perfect sense, should be easier than I thought in the past now I understand things a little better, and has loads of pluses. So I'm possibly not giving your solutions quite the concentration they deserve. But thanks. Hopefully I've learned. – Kevin4fm Jun 8 '15 at 21:03
  • Please understand, I do think that a custom field or a taxonomy is a better way to do this than hacking the post title or slug. You should be thinking hard about those solutions. – s_ha_dum Jun 8 '15 at 21:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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