0

Consider this: I've got a custom post type 'portfolio' these posts, or "portfolio items" all have several categories such as: "photography", "concepting", "branding" , "clientA" , "clientB" etc.

Now I want to build a custom backend sollution to let the user create a pool of "Special categories" which I eventualy want to use to create a query containing every "portfolio item" that has at least one of these categories.

So for example: I have the following portfolio items with the following categories:

  1. "Item1" - "photography","concepting","clientA"
  2. "Item2" - "concepting","clientB"
  3. "Item3" - "branding","clientC"

The sollution I want to create for the backend should let the user create an array of categories, where they can choose from every category that is linked to a portfolio item.

So If the user added: "clientA" and "clientB" to this sollution, then I want my query to return "Item1" & "Item2".

How would I go about creating a tool for the user that lets them fill an array of categories, that I can later use to define a query?

I think I need to create a custom menu page that lists all categories linked to a post and then via checkmarks create the array of categories. But I'm not sure how to create and store this array.

My actual plugin based on Pabamato's answer:

add_action( 'admin_menu', 'show_specific_cats' );    

function show_specific_cats() {
    add_menu_page('Kies de categories', 'Manage categories', 'manage_options', 'show-spec-cats', 'category_options');
}

function category_options() {
    if ( !current_user_can( 'manage_options' ) )  {
        wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
    }

    $categories = get_terms( 'category', 'orderby=count&hide_empty=0' );

    if($_POST) {
        $theCats = [];
        foreach($_POST  as $key => $value) {
            array_push($theCats , $value);
        }
        update_option( 'show_these_cats', $_POST );
    }

    $showTheseCats = get_option('show_these_cats');

    echo '<form method="post">';
        echo '<input type="hidden" name="empty" value="0" />';
        echo '<ul>';
        foreach($categories as $key => $value){
            echo '<li>'.
                '<input type="checkbox" name="checkbox[]"';
            if($showTheseCats['checkbox']){
                if(in_array($value->term_id, $showTheseCats['checkbox'])) {
                    echo 'checked';
                }
            }
            echo
                ' value="'.
                $value->term_id.
                '">'.
                $value->name.
            '</li>';
        }
        echo '</ul>';
        echo '<input type="submit" value="Opslaan">';
    echo '</form>';
}

The array of categories can be called via the custom option show_these_cats.

My query on page is now like this:

    $showTheseCats = get_option('show_these_cats');
    if($showTheseCats[checkbox]){
        $args = 'cat='. implode(', ', $showTheseCats[checkbox]).'';
        query_posts( $args );

        while ( have_posts() ) : the_post();
            echo '<li>';
            the_title();
            echo '</li>';
        endwhile;
        wp_reset_query();
    }

1 Answer 1

1

You can use

https://codex.wordpress.org/Function_Reference/get_terms

to return the needed data for the select, and store the selected values (term IDs) in options table using

https://codex.wordpress.org/Function_Reference/update_option

The 'hide_empty' param in 'get_terms' will prevent returning categories with no posts.

1
  • This is an awesome idea. I'll update my question with my final plugin once I Implemented your idea. Oct 27, 2015 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.