DiscipleTools / DiscipleTools/disciple-tools-theme

Sorting by 'tags' field causes duplicate records in list tables

Open
#2,929 1 comment 1 reaction 0 assignees View on GitHub
bug
Dominant language
PHP
Stars
48
Forks
66
PR merge metrics
No merged PRs in 30d

Description

**Describe the bug**
Sorting by `tags` custom fields causes duplicate posts in `search_viewable_post`. Posts with values in the Tags field are duplicated based on the number of tags saved in the field for that post. Example: Journey A has 3 selected tags in my Tags field, 'blue', 'green', and 'red'. Journey B has none. When I sort by Tags, Journey A shows up 3 times in the list while Journey B shows only once.

**To Reproduce**
Steps to reproduce the behavior:
1. Go to the Contacts tab
2. Add a Tags field to the displayed list
3. Click on the Tags field in the table to sort by that field
4. If a post has X tags in the Tags field you are sorting by, it will be displayed X times in the list.
**Expected behavior**
Each post should only be returned one time.

**Disciple.Tools Theme:**
- version: 1.82.2
- setup: local

Below is a summary given from Gemini that goes into more detail. The suggested fix is to remove the problematic code from posts.php, but likely we will just want to create a case to handle tags fields (like the existing multi_select case) detailed below. `journey_category` mentioned below is the tags field I used in testing.

```Bug Report: Sorting by tags custom fields causes duplicate posts in search_viewable_post
Issue Description
When querying posts via the REST API and sorting by a multi-value custom field, the API returns duplicate records based on the number of values assigned to the post.

Sorting by a tags field (like journey_category) duplicates the post in the result set for every tag it possesses.
However, sorting by a multi_select field (like journey_roles) correctly returns unique, deduplicated posts.
Root Cause Analysis
The duplication stems from how the SQL GROUP BY clause is constructed inside the search_viewable_post core function.

For multi_select fields, the $sort_sql generates unique table aliases inside a CASE statement (e.g., $alias.meta_value).
For tags fields, the logic falls to the default else block, which explicitly assigns sort.meta_value to the $sort_sql.
Later in the function, an if ( strpos( $sort_sql, 'sort.meta_value' ) !== false ) check forces , sort.meta_value into the $group_by_sql.
By grouping by both p.ID and sort.meta_value, the database treats every unique tag as a separate group, returning multiple rows for a single post.
Suggested Fix To ensure list_posts and search_viewable_post always return unique post entities regardless of the field type, the GROUP BY clause should strictly group by p.ID.
Removing the $group_by_sql modification resolves the issue globally:
PHP:
$group_by_sql = '';
// Removing this condition prevents row duplication on multi-value fields
// if ( strpos( $sort_sql, 'sort.meta_value' ) !== false ){
// $group_by_sql = ', sort.meta_value';
// }
```

Contributor guide

Open the contributing guide

Research direction

Start in posts.php at the search_viewable_post logic described in the issue, then compare the tags-field handling with the existing multi_select case. Reproduce the Contacts list sorting by a Tags field and verify that each post appears once, including posts with multiple tags.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend-api-design, databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.