0

I'm creating a website where I need to 'subscribe' client Pages to certain categories.

For example:

On example.com/client/johnson-co/ show all posts in the categories 'GST', 'Estate' and 'Tax General'.

On example.com/client/smith-co/ show all posts in the categories 'GST', 'Insurance' and 'Personal'.

I'm not at all attached to using Pages if there's a better way :) I do however need the categories 'subscribed' to to be editable using WordPress admin; not hard-coded into the theme.


I've researched and experimented with the following methods to no avail:

  • Giving a category multiple parents (currently not possible in WP) - this would let me give category gst a parent of johnson-co and smith-co
  • Using query_posts() and including cat=x (as per the codex) - this works but is hard coded and doesn't let someone subscribe a page to categories in the admin.
  • Using custom post type (set up using CCTM plugin) so /client/johnson-co/ has checkbox meta fields of GST, Estate, Insurance, Personal etc. This is the closest but I couldn't get the link between what was checked and the categories showing working, and it feels like a bit of an ugly hack.

Would hugely really appreciate any ideas!

2
  • are client pages a custom post type or just vanilla pages?
    – Milo
    Mar 19 '13 at 22:07
  • I've tried both, but currently they're custom post types.
    – jam
    Mar 19 '13 at 22:08
0

Enable the category taxonomy for your custom post type, or enable it for the default page post type:

function wpa_cats_for_pages(){
    register_taxonomy_for_object_type( 'category', 'page' );
}
add_action( 'init', 'wpa_cats_for_pages' );

Then within your page template, get the category IDs assigned to the client page and create a new query which passes those IDs:

// get IDs of categories assigned to this client page
$categories = wp_get_object_terms( $post->ID, 'category', array( 'fields' => 'ids' ) );

// query for posts with categories that match those assigned to this page
$args = array(
    'posts_per_page' => -1, // get all matching posts
    'category__in' => $categories
);

$client_posts = new WP_Query( $args );

// output post data
while( $client_posts->have_posts() ):
    $client_posts->the_post();
    the_title();
endwhile;

// reset the global $post object so template tags for this page work as expected
wp_reset_postdata();

Then for each client page, you'll just select the categories of posts you want displayed on that page, the same way you assign categories to the posts.

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.