Joe Bop
Joe Bop

Reputation: 3

How to use a relationship in the where parameter when finding records using the pods framework?

my question has to do with the pods framework plugin for wordpress sites. I am using pods version 2.2 and have been having trouble with the where parameter in the find() function.

I'd be suprised if I am the first person to encounter this problem but I have searched extensively and haven't found anyone providing an answer (or question at that).

Anyway, I'll give an example to highlight my problem.

Say I have a Bands Pod and a Records Pod and these two pods have a bi-directional multi-select relationship between them (that is, an n to n relationship). Hence a band can have numerous records and a record can have multiple bands. Further, the relationship exists between the fields BandsPod('records') and RecordsPod('bands').

Now, I can retrieve all records in the records pod like so (note the where is commented out):

$pods = pods('records');

$params = array(
                'select' => 't.*',
                'limit' => -1
                //,'where' => '???'
               );
$pods->find($params);

Then do whatever I want with them, e.g. template(), fetch(), etc.

My trouble occurs when I want to filter by band. What should my where statement be if I want to retrieve all records by band with id 1?

My view is that it should be something like this:

$params = array(
                'select' => 't.*',
                'limit' => -1,
                'where' => '1 IN bands.id'
               );
$pods->find($params);

However, this does not work (not that I especially expected it to).

It would also be desirable to know how this would work for filtering by multiple bands. E.g.

'where' => '(1, 2) IN bands.id'

As I said before, I've been trying to get this to work for some time and with little joy. I have managed to get it working but in a very ugly way. I.e.

  1. Get bands from Bands Pod with relevant band ids,
  2. Collect all ids of records from the bands pod records field,
  3. Write params for Records Pod
  4. Use the ids in the where statement to check they match with t.id,
  5. $pods->find(); //where $pods=pods('records');

Thanks in advance for taking the time to read this and any answers you may give.

Cheers, Joe

N.B. I know about the filters() function and it does not do what I'm after. I'd like stuff specifically for the where statement please.

Upvotes: 0

Views: 3146

Answers (1)

Scott Kingsley Clark
Scott Kingsley Clark

Reputation: 987

You would want:

'where' => 'bands.id IN ( 1, 2 )'

If your bands are a custom post type, it would be:

'where' => 'bands.ID IN ( 1, 2 )'

Upvotes: 0

Related Questions