Support

Account

Home Forums General Issues Archive page custom fields filter (probs with tax and Post Obj fields)

Solved

Archive page custom fields filter (probs with tax and Post Obj fields)

  • Hello!

    I have 2 custom post types – projects and designers. project item has custom fields (text, taxonomies and Post Object). Text field contain area of flat/apartment, taxonomies: first, it’s type of project and, second one is status of project (prototype, finished, etc.). Post Oblect field – relationship Project and Designer (select a designer for project).

    I’m making a filter for Projects Archive page. Like this – https://www.advancedcustomfields.com/resources/creating-wp-archive-custom-field-filter/

    It’s working good for simple text custom field (for area), but not working for taxonomies and post object fields. How should I make this?

    my urls for filtering on the archive page:
    http://mydomain.com/projects/?area=25
    http://mydomain.com/projects/?designer=new_studio
    http://mydomain.com/projects/?type=bath

    My functions.php

    add_action('pre_get_posts', 'my_pre_get_posts');
    
    function my_pre_get_posts( $query) {
    
        if( is_admin() ) {
            return;
        }
    
        $meta_query = $query->get('meta_query');
    
        if (isset($_GET['area']) ) {
    
            $meta_query[] = array(
                'key' => 'project_area', // custom text field - working good
                'value' => $_GET['area'],
                'type'      => 'NUMERIC',
                'compare' => '=',
            );
        }
    
           if (isset($_GET['designer']) ) {
    
            $meta_query[] = array(
                'key' => 'project_designer_obj', //custom field (Post obj) - not working. How should I use ['post_name'] of this Object for comparing with name of designer below?
                'value' => $_GET['designer'],
                'compare' => '=',
            );
        }
    
        if (isset($_GET['type']) ) {
    
            $meta_query[] = array(
                'key' => 'project_type',  //custom field (taxonomy) - not working. How should I use relationship taxs?
                'value' => $_GET['type'],
                'compare' => '=',
            );
        }
    
        $query->set('meta_query', $meta_query);
    
        return;
    
    }

    If I try this:

    if( !empty($_GET['designer']) ){
        $designer = the_field('project_designer_obj');
        $designer_name = $designer->post_name; // Trying to get property of non-object
        $meta_query[] = array(
           'key'       => 'project_designer_obj',
            'value'     => $designer_name,
            'compare'   => '=',
        );
    }

    I see “Notice: Trying to get property of non-object …”

    Please, help me figure it out.

  • Taxonomy fields store term IDs, not the name of the term. In addition to this they store arrays of values.

    so something like this /projects/?designer=new_studio would need to look something like this /projects/?designer=22 (id value is just an example)

    And then the query would need to look something like this

    
    $meta_query[] = array(
      'key' => 'project_designer_obj',
      'value' => '"'.$_GET['designer'].'"', // quotes around id important
      'compare' => 'LIKE',
    );
    
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Archive page custom fields filter (probs with tax and Post Obj fields)’ is closed to new replies.