简体   繁体   中英

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.

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..)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM