Pretty sure my code is close to working, but I am having some difficulty. I’m using WordPress, which has some functions in the first foreach loop to grab the pages that the current user authored, but I know that part works. My focus is on the second loop, which is intended to make an array comprised of unique values (page categories).
Have had trouble responding to users directly on here before (computer too old to update browser) so thanks ahead of time for the help!
$args= array('sort_column' => 'post_date', 'sort_order' => 'desc', 'authors' => $current_user -> user_login;
$pages = get_pages($args);
$uniques = array();
foreach ($pages as $page) {
$categories = get_the_category($page->ID);
foreach ($uniques as $unique) {
if ( in_array($categories[0]->name, $unique) )
{
continue;
}
array_push($unique, $categories[0]->name);
echo end($unique[0]);
echo '<br>';
echo $categories[0]->name;
}
}
You have a problem with your
foreach ($uniques as $unique) {
line. That loop will never execute since$uniques
is always empty.Resolved, thanks for your help!
}
I am getting correct that you wish to get all the unique values of categories in the array
$uniques
and$categories[0]->name
is fetching the category name for you.Then try using the
array_unique()
function of php then you might usearray_values()
to sort the indexes.This will get you only the first category from all the pages as we are only inserting 0 index. To get all the categories of all pages use below code