• Resolved mike.chiv

    (@mikechiv)


    I have been searching around but haven’t found a solution that seems to cover my whole problem.

    On the “Stock” page of the site I am working on, I need to produce a drop menu of stock “categories”. The categories are stored as the values of custom fields on the child pages of the “Stock” page (“Stock Item” pages). The issue arises from the fact that each child page could have several values for the “categories” custom field.

    As far as I can work out, I need to create an array of the field values for each child page for example by separating them with commas and using “explode”, and then use those arrays to create a new array, i.e. the complete list of categories from all child pages of “Stock”.

    I then need to remove duplicate values from this list, leaving only one instance of each existing category. I believe this part is possible using the php, array_unique().

    If anyone has any solutions to this problem, I would be eternally grateful! I am pretty new to both php and wordpress, so please make any explanations as straightforward as possible.

    Thanks in advance.

Viewing 1 replies (of 1 total)
  • Thread Starter mike.chiv

    (@mikechiv)

    Never mind! I fixed it after a load of research and trial and error.

    As each product page used the same category, I was able to create an array of these pages, then create another array of the exploded values for “category” from each page using nested foreach loops.

    I then used the function, “array_unique” to remove duplicates from the list. Please excuse the simplistic explanation as well as code, I wrote it for people like myself who need the extra detail!

    <?php
    $pages = get_posts(array(
    		'post_type' => 'page',
    		'meta_key' => '_wp_page_template',
    		'meta_value' => 'product.php'
    ));
    
    $catlist = array();
    
    foreach($pages as $page){
    $categories = get_post_meta($page->ID, 'categories', true);
    $categories = explode(',' , $categories);
    	foreach($categories as $category){
    			$catlist[] = $category;
    	}
    } 
    
    $unique_catlist = array_unique($catlist);
    
    ?>
Viewing 1 replies (of 1 total)
  • The topic ‘Drop menu from custom field values of several child pages’ is closed to new replies.