WordPress.org

Ready to get started?Download WordPress

Forums

CPT-onomies: Using Custom Post Types as Taxonomies
[resolved] Can you attach a CPT-onomy to Pages? (5 posts)

  1. fosterbydesign
    Member
    Posted 1 year ago #

    First of all, thanks for putting together such a useful plug-in. It has really made a difference in how I work with WordPress.

    Here's my scenario.

    I've created a custom post type called Resources (intended to hold things like white papers, datasheets, etc.). I've also created post types for Features, Partners, Solutions, and more.

    I edited the Features post type and checked Resources in the Attach to Post Types section. This means our marketing person can now upload a Resource, and while editing it they can "tag" which Features, Partners, Solutions pages it will show up on.

    (Note: obviously a little custom code used to display a "related resources" widget based on the current post type and what resources have been tagged to it).

    All good so far until I realized that Resources should show up on Pages too. Single the plug-in won't let you edit the main post types, I don't see a way to assign a CPT-onomy for Resources to Pages.

    I really could use this - can you provide any ideas on how to accomplish? I'm comfortable with a coding solution if you can point me in the right direction.

    Many thanks...

    --Mark

    http://wordpress.org/extend/plugins/cpt-onomies/

  2. Rachel Carden
    Member
    Plugin Author

    Posted 1 year ago #

    Mark,

    I'm glad you like CPT-onomies so much! It is definitely a labor of love.

    There is a programmatic way of registering CPT-onomies. Try this out:

    add_action( 'wp_loaded', 'my_website_register_cpt_onomy' );
    function my_website_register_cpt_onomy() {
       global $cpt_onomies_manager;
       if ( $cpt_onomies_manager ) {
          $cpt_onomies_manager->register_cpt_onomy( 'resources', 'page', array( 'restrict_user_capabilities' => array( 'administrator' ) ) );
       }
    }

    The last variable is an arguments array that you'll probably want to customize. Check out the CPT-onomies documentation for more information.

  3. fosterbydesign
    Member
    Posted 1 year ago #

    Thanks for the fast response!

    For my purposes (adding 'page' checkmarks while editing a 'resource' post), I needed to reverse 'resources' and 'page' in your register call.

    Now editing a resource works as expected. And that's awesome.

    Small issue remains:

    When I use WP_Query to list all Resources that contain a 'page' with the current page slug name, it is returning all resources. This is ONLY happening on 'page' - the same exact code works for all other custom post types I've created.

    It sounds like the bug they fixed in WordPress 3.5:
    http://core.trac.wordpress.org/ticket/20604

    Any ideas?

    Here is a small snippet:

    global $post;
    $args = array (
    'posts_per_page' => '-1',
    'post_type' => 'resource',
    'orderby' => 'menu_order', 'order' => 'ASC',
    'page' => 'my-test-page'
    );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();

    // Some posts (i.e. resources) come back with the 'page' taxonomy empty, despite the filter above - which should not be happening.
    Appreciate any pointers!

  4. fosterbydesign
    Member
    Posted 1 year ago #

    OK, I tracked it down in case anyone else finds this useful.

    It appears that 'page' will not work as a "simple" taxonomy query.

    Instead of this:

    'page' => 'my-test-page'

    I used this:

    'tax_query' => array( array( 'taxonomy' => 'page','field' => 'slug', 'terms' => 'my-test-page'))

    Hope that helps!

    --Mark

  5. Rachel Carden
    Member
    Plugin Author

    Posted 1 year ago #

    Awesome! Thanks Mark!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic