Support

Account

Home Forums Backend Issues (wp-admin) Using get_field_object to create custom filters in wp-admin

Unread

Using get_field_object to create custom filters in wp-admin

  • Hi,

    I want to add custom filters to my podcasts post type in wp-admin.

    It almost works 🙂
    The problem is that after selecting a value in one of these fields, the filter disappears, and get_field_object returns null.

    Any idea why ? I tried to use get_the_id and so on, but still no luck.

    // Add custom filters to the podcast post type
    function custom_podcast_filters() {
      global $typenow;
      
      if ($typenow === 'podcast') {
        
        $podcast_home_switch = get_field_object('podcast_home_switch',get_the_ID());
        $podcast_recommended_switch = get_field_object('podcast_recommended_switch',get_the_ID());
        $podcast_popular_switch = get_field_object('podcast_popular_switch',get_the_ID());
        $wp_author_id = get_field_object('wp_author_id',get_the_ID());
        $wp_program_id = get_field_object('wp_program_id',get_the_ID());
    
        var_dump($podcast_home_switch);
    
        // Filter by podcast_home_switch
        if ($podcast_home_switch) {
          $selected_podcast_home_switch = isset($_GET['podcast_home_switch']) ? $_GET['podcast_home_switch'] : '';
          echo '<select name="podcast_home_switch">';
          echo '<option value="">Filtruj - Na głównej</option>';
          echo '<option value="true" ' . selected($selected_podcast_home_switch, 'true', false) . '>Tak</option>';
          echo '<option value="false" ' . selected($selected_podcast_home_switch, 'false', false) . '>Nie</option>';
          echo '</select>';
        }
    
        // Filter by podcast_recommended_switch
        if ($podcast_recommended_switch) {
          $selected_podcast_recommended_switch = isset($_GET['podcast_recommended_switch']) ? $_GET['podcast_recommended_switch'] : '';
          echo '<select name="podcast_recommended_switch">';
          echo '<option value="">Filtruj - Polecany</option>';
          echo '<option value="true" ' . selected($selected_podcast_recommended_switch, 'true', false) . '>Tak</option>';
          echo '<option value="false" ' . selected($selected_podcast_recommended_switch, 'false', false) . '>Nie</option>';
          echo '</select>';
        }
    
        // Filter by podcast_popular_switch
        if ($podcast_popular_switch) {
          $selected_podcast_popular_switch = isset($_GET['podcast_popular_switch']) ? $_GET['podcast_popular_switch'] : '';
          echo '<select name="podcast_popular_switch">';
          echo '<option value="">Filtruj - popularny</option>';
          echo '<option value="true" ' . selected($selected_podcast_popular_switch, 'true', false) . '>Tak</option>';
          echo '<option value="false" ' . selected($selected_podcast_popular_switch, 'false', false) . '>Nie</option>';
          echo '</select>';
        }
    
        // Filter by wp_author_id
        if ($wp_author_id) {
          $selected_author_id = isset($_GET['wp_author_id']) ? $_GET['wp_author_id'] : '';
          echo '<select name="wp_author_id">';
          echo '<option value="">Filtruj - autorzy</option>';
          $authors = get_posts(array(
            'post_type' => 'zespol',
            'numberposts' => -1,
            'post_status'    => 'publish',
            'meta_key'      => 'team_sort_id',
            'meta_type' => 'NUMERIC',
            'orderby'      => 'meta_value_num',
            'order' => 'ASC',
            'meta_query' => array(
                array(
                    'key' => 'podcaster_switch',
                    'value'     => '1',
                    'compare'   => '=='
                )
            ),
          ));
    
          foreach ($authors as $author) {
            $author_id = $author->ID;
            $author_name = $author->post_title;
            echo '<option value="' . $author_id . '" ' . selected($selected_author_id, $author_id, false) . '>' . $author_name . '</option>';
          }
          echo '</select>';
        }
    
        // Filter by wp_program_id
        if ($wp_program_id) {
             $selected_program_id = isset($_GET['wp_program_id']) ? $_GET['wp_program_id'] : '';
            echo '<select name="wp_program_id">';
            echo '<option value="">Filtruj - audycje</option>';
            $programs = get_posts(array(
            'post_type' => 'audycja',
            'numberposts' => -1,
            'post_status'    => 'publish',
            'meta_key'      => 'program_sort_ids',
            'meta_type' => 'NUMERIC',
            'orderby'      => 'meta_value_num',
            'order'        => 'ASC',
            ));
            foreach ($programs as $program) {
                $program_id = $program->ID;
                $program_name = $program->post_title;
                echo '<option value="' . $program_id . '" ' . selected($selected_program_id, $program_id, false) . '>' . $program_name . '</option>';
            }
                echo '</select>';
            }   
        }
    }
    add_action('restrict_manage_posts', 'custom_podcast_filters');
Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.