Search code examples
mysqlwordpresspodscms

Wordpress Pods: How to only get (sub)categories with articles in it


I am trying to get all subcategories which have articles linked to it.

To visualize the needed result:

  • subcategory A (10 articles linked)
  • subcategory B (0 articles linked) <- I do not want this one
  • subcategory C (1 article linked)

So for my pods , I have the following: (all Advanced Content Type)

Category pod:

  • Text field: slug
  • Relational field: category_relation (this field is only filled when the category is a subcategory)

Article pod:

  • Relational field: category

I have this query, which works, but it does not skip the subcategories who has no articles in it.

$subcategories = pods( 'category',
    array( 'where' => "category_relation.slug = 'Top Category" 
) );

I know I should do a left join, but I do not see how to get that working because I can only select one pod type at once.

Have anyone experience with this type of getting pods data?

Note: I asked this question on the Pods forum too, but did not get any response. On SO I will reach more people I think and update the forum post on Pods.io with the best answer.


Solution

  • Have you tried 'where' => 'category_relation.slug = "Top Category" AND 0 < tt.count' yet?

    Also, if you pass any string into the 'where' that's a $variable, definite be sure to sanitize it first (pods_sanitize( $variable ), $wpdp->prepare( "category_relation.slug = %s", $variable ), etc..)