Support

Account

Home Forums ACF PRO Media fields dissapear when using posts_where and posts_join

Solving

Media fields dissapear when using posts_where and posts_join

  • I have the following code, which works great. But when I use it, my custom fields are not showing anymore.

    Why is this and how can I fix this?

    function filter_where( $where ) {
    global $current_user;
    global $wpdb;
    	if( is_user_logged_in() && is_admin()){
    		if( isset( $_POST['action'] ) ){
    			if( $_POST['action'] == 'query-attachments' ){
    				$where .= " AND (($wpdb->postmeta.meta_key = 'mediatoegang' AND $wpdb->postmeta.meta_value LIKE '%administrator%')) ";
    			}
    		}
    	}
    	return $where;
    }
    add_filter('posts_where', 'filter_where');
    
    function custom_posts_join($join){
         global $wpdb;
    	if( is_user_logged_in() && is_admin()){
         $join .= " LEFT JOIN $wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id ";
    }
         return $join;
    }
    add_filter( 'posts_join' , 'custom_posts_join');
  • Because you are altering every query run by $wpdb that meets the conditions and you’re probably effecting other queries that you aren’t meaning to alter.

  • Well yes and no. When I edit my media on the /upload.php?mode=list url everything is OK.

    But when i edit my media on the /upload.php?mode=grid url the fields are gone. Allso when i try to add media the fields are gone.

    Kinda weird right?

  • The media grid layout works differently than other things in WP. I’ve had trouble with it myself. It has to do with it being a modal window…. I never did get my problems worked out.

    My suggestion would be to use error_log() to output some stuff into the error log so you can see what’s happening and why.

    Turn on the error log in wp-config.php

    
    define('WP_DEBUG', true );
    define('WP_DEBUG_DISPLAY', false);
    define('WP_DEBUG_LOG', true);
    

    then do something like this

    
    function filter_where( $where ) {
    global $current_user;
    global $wpdb;
    	if( is_user_logged_in() && is_admin()){
    		if( isset( $_POST['action'] ) ){
    			if( $_POST['action'] == 'query-attachments' ){
    				$where .= " AND (($wpdb->postmeta.meta_key = 'mediatoegang' AND $wpdb->postmeta.meta_value LIKE '%administrator%')) ";
    			}
    		}
    	}
    	// write where to error log
    	error_log($where);
    	return $where;
    }
    add_filter('posts_where', 'filter_where');
    

    then you can look into the error log and see what you put there. You can also log other things to find out what’s going on.

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Media fields dissapear when using posts_where and posts_join’ is closed to new replies.