0

I have a site with around 8000 products, with a slightly poor site structure - instead of custom taxonomies the Brand Name and Retailer are Parent Categories with the various individual brands, and retailers as sub categories.

Brands (Cat ID 187 - around 300 brands as sub cats)

  • Brand 1
  • Brand 2
  • Brand 3 etc

Retailers (Cat ID 186 - around 20 retailers as sub cats)

  • Retailer 1
  • Retailer 2
  • Retailer 3 etc

I have a list of the individual brands and retailers individual subcat IDs, and for site maintenance was wondering if there was a way to display all posts which are accidentally assigned to more than one retailer, or more than one brand?

I've found solutions for locating all posts assigned to 2 specific categories, but not any that can display posts which appear in any 2 of the categories featured in a long array.

Or alternatively any posts which belong to 2 or more sub categories of a certain parent ID - if that's less complicated than one long array of sub cat IDs?

It doesn't matter if it's slow, as it's only me who'll be seeing the output in order to maintain the website.

If anyone can give me any pointers, I'd be very grateful - Thank you in advance :)

Joey

Edit: The only quick solution I can think of would be to output all posts IDs with a comma separated list of categories that they belong to, then remove all the non relevant categories with search and replace (or do this with the initial query), and then import this data into Excel and see which posts had more than 2 columns for Brand and Retailer? Was wondering if there was a more elegant solution than this? Thanks

2

I think I'd setup a loop for all posts and the do something like this inside the loop:

$post_cats = get_the_category();
$brand_cats = 0;
$retail_cats = 0;
foreach ( $post_cats as $post_cat ) {
    if ($post_cat->category_parent == 187) {
        $brand_cats++;
    if ($post_cat->category_parent == 186) {
        $retail_cats++;
    }
}
if ( $brand_cats > 1 )
    echo "Duplicate Brand for Post #".get_the_ID;
if ( $retail_cats > 1 )
    echo "Duplicate Retailer for Post #".get_the_ID;

The code is untested but should give you the general approach.

  • Thank you so much for this Colin, I'll have a go with this and see what happens :) – Joey Feb 10 '18 at 7:38
0

Thanks to Colin, I've managed to work the following answer:

 <?php
$id = get_the_ID();
$post_cats = get_the_category();
$brand_cats = 0;
$retail_cats = 0;
foreach ( $post_cats as $post_cat ) {
    if ($post_cat->category_parent == 187) {
        $brand_cats++; }
    if ($post_cat->category_parent == 186) {
        $retail_cats++;
    }
}
if ( $brand_cats > 1 )  {
 echo "Multiple Brands ";
    echo $id;
    echo "<br>";
   } else {
    echo "";
}

if ( $retail_cats > 1 )  {
 echo "Multiple Retailers ";
    echo $id;
    echo "<br>";
   } else {
    echo "";
}
    ?>

and then use that within WP Query to select the posts I need to search - this outputs a list of post IDs together with whether the issue is too many retailer categories, or too many brands.

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.