Home Forums ACF PRO Archive.php with custom field filter (object post) Reply To: Archive.php with custom field filter (object post)

  • Thank you, can not imagine what relief dates when you solve a problem that blocks an entire project like mine.

    To put chronicle the complete function for this specific use.

    • my custom post archive : ‘newstand’
    • my custom field: ‘newstand_selettore_stand’ (Object post one choose or multi choose for only custom post “stand”)
    • a ID value for a stand custom post exemple : 3640

    Result on the URL of site:
    my url for archive page:

    The function:

    function my_pre_get_posts_newstand( $query ) {
        // validate
        if( is_admin() )               { return; }
        if( !$query->is_main_query() ) { return; }
        // get original meta query
        $meta_query = $query->get('meta_query');
            // allow the url to alter the query
            // eg:
            // eg:
            // eg  http://www.luccafan/newstand/?newstand_selettore_stand=3640
            if( !empty($_GET['newstand_selettore_stand']) ){
                    $newstand_selettore_stand = explode(',', $_GET['newstand_selettore_stand']);
                    if( !empty($newstand_selettore_stand) ) {
                        foreach( $newstand_selettore_stand as $id ) {
                                $meta_query[] = array(
                                    'key'       => 'newstand_selettore_stand',
                                    'value'     => $id,
                                    'compare'   => 'LIKE',
            }// end of !empty($_GET['newstand_selettore_stand'])
        // update the meta query args
        $query->set('meta_query', $meta_query);
        // always return
    add_action('pre_get_posts', 'my_pre_get_posts_newstand');