admin管理员组

文章数量:1277902

I have 3 custom post types, which are related to each other. Projects, Positions and Feedbacks. 1 Project has many Positions and 1 Position has many Feedbacks.

Relations are defined with meta values. Feedbacks post has a meta value, where the value points to position post id, and Positions post has a meta value, where the value points to project post id.

I want to create a custom filter in Feedback admin list, where rows are filtered by project.

It would be nice to do it with a 'parse_query' filter, since I already have some filters attached to it.

How to achieve this?

I have 3 custom post types, which are related to each other. Projects, Positions and Feedbacks. 1 Project has many Positions and 1 Position has many Feedbacks.

Relations are defined with meta values. Feedbacks post has a meta value, where the value points to position post id, and Positions post has a meta value, where the value points to project post id.

I want to create a custom filter in Feedback admin list, where rows are filtered by project.

It would be nice to do it with a 'parse_query' filter, since I already have some filters attached to it.

How to achieve this?

Share Improve this question edited Aug 18, 2021 at 14:08 Hannes Härm asked Aug 18, 2021 at 10:28 Hannes HärmHannes Härm 12 bronze badges 2
  • What have you tried so far? – Mat Commented Aug 20, 2021 at 15:00
  • I have found a semi solution. Ill post it as one of answers – Hannes Härm Commented Aug 20, 2021 at 23:14
Add a comment  | 

1 Answer 1

Reset to default 0

One solution may be to get all position ids, where project id matches and compare them with the 'OR' comparer.

$position_ids = get_positions_in_project($project_id);

foreach ($position_ids as $id) {
    $meta_query[] = array(
        'key' => 'feedback_position_relation',
        'value' => $id,
        'compare' => '=',
    );
}

$meta_query['relation'] = 'OR';
$query->set('meta_query', $meta_query)

But the problem with this is, that meta query does not accept complex parameters. I want a feedback where the position id is 1 and project id is 2. The relation will always be 'OR'.

In a simple problem this solution should give the answer. For my sake this solution does not work, since i want to give the user the ability to filter other parameters too.

I have searched for 'in array' meta_query, but this seems to be not supported.

UPDATE

With wordpress 5.8.1 (I have no idea if earlier versions work), the IN Array comparison works.

if ($_GET['project'] != 'all') {
        $project_id = $_GET['project'];
        $positions = get_position_ids_by_project($project_id);
        $meta_query[] = array(
            'key' => 'summary_summary_position',
            'value' => $positions,
            'compare' => 'IN',
        );
    }
$query->set('meta_query', $meta_query)

本文标签: