Support

Account

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

Solved

Archive.php with custom field filter (object post)

  • Hi,
    I need to create an archive.php filtered by a custom field
    I followed the guide that you have entered, and I have focused in particular on function in function.php page ( my_pre_get_posts).

    But I have a problem, my custom field is not a sequence of options that can only be those; but I have an array.

    then
    my custom post archive : ‘newstand’
    my custom field: ‘newstand_selettore_stand’ (Object post for only custom post “stand”)
    a ID value for a stand custom post exemple : 3640

    my url for archive page: http://mydomain.com/newstand/?newstand_selettore_stand=3640

    The function:

    
    add_action('pre_get_posts', 'my_pre_get_posts');
    function my_pre_get_posts( $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: http://www.website.com/events?location=melbourne
            // eg: http://www.website.com/events?location=sydney
            if( !empty($_GET['newstand_selettore_stand']) ) {
                
            $newstand_selettore_stand = explode(',', $_GET['newstand_selettore_stand']);
                
    
    $meta_query[] = array(
                    'key'       => 'newstand_selettore_stand',
                    'value'     => $newstand_selettore_stand,
                    'compare'   => 'LIKE',
    
                );
            } 
    
        // update the meta query args
        $query->set('meta_query', $meta_query);
    
        // always return
        return;
    
    }
    

    With this function, the values ​​you see in the url for example. but I am not filtered by id of the other type of post (object post)
    I’m also appears before loading error:

    Warning: trim() expects parameter 1 to be string, array given in http://www.mydomain.com/wp-includes/meta.php on line 107

    I then thought to directly call ID of the custom field with this code

     $valore = get_field('newstand_selettore_stand');
      $meta_query[] = array(
                    'key'       => 'newstand_selettore_stand',
                    'value'     => $valore->ID,
                    'compare'   => 'LIKE', 
    

    In this way, the initial error disappears, but the fact remains that they are visualzizati all the posts and are not filtered by the ID of the custom post.

    The main problem, I think, is that it is a method php ($ _GET [‘newstand_selettore_stand’]) that does not allow call ID array inside.
    But it is also true that when call ID from custom code field as the last, is not received nothing.

    I do not know how to do, you have any suggestions?

    Also, if you know the answer, you might consider the problem having a custom field with multi selection Object post?

    Thanks for your attention

  • Hi @bibi.lerici

    The PHP error explains it quite well:
    Warning: trim() expects parameter 1 to be string, array given in http://www.mydomain.com/wp-includes/meta.php on line 107

    This is telling you that you can’t use an array as your meta compare:

    
    $newstand_selettore_stand = explode(',', $_GET['newstand_selettore_stand']);
                
    
    $meta_query[] = array(
                    'key'       => 'newstand_selettore_stand',
                    'value'     => $newstand_selettore_stand,
                    'compare'   => 'LIKE',
    
                );
    

    Instead, loop through the array and add them like so:

    
    <?php 
    
    $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',
    
            );
    		
    	}
    	
    }
    
     ?>
    
  • 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: http://mydomain.com/newstand/?newstand_selettore_stand=3640

    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: http://www.website.com/events?location=melbourne
            // eg: http://www.website.com/events?location=sydney
            // 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
        return;
    
    }
    add_action('pre_get_posts', 'my_pre_get_posts_newstand');
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Archive.php with custom field filter (object post)’ is closed to new replies.