Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

hovida on "WP_Query - Meta with Arrays"

$
0
0

Hey guys,

i try to work a little bit with meta_query on the pre_get_posts action and i have a little Problem.

I have a custom Post-Type "news". On the edit-page i had added a custom meta box. Here the user can select the visibility of the news. For example:
Metabox Screenshot

Options (postmeta and usermeta) will be stored as Array (serialized) in the metadata, like:
PMA - Postmeta
PMA - Usermeta

But how i can combine the meta-querys with Arrays?

add_action('pre_get_posts',						array($this, 'pre_get_posts'));

function pre_get_posts($query) {
			if($query->query_vars['post_type'] == 'news') {
				$user				= wp_get_current_user();
				$user_company		= get_user_meta($user->ID, '_user_company', true);
				$user_company		= empty($user_company) ? array() : (array) $user_company;
				$user_company[]		= 'all';
				$user_company[]		= 'front';

				$query->set('meta_query', (array(array(
					'key'		=> '_news_visibility',
					'value'		=> $user_company,
					'type'		=> 'array'
				))));
			}

			return $query;
		}

Data - The _news_visibility is an array. For example:
array( 'all', 1 )

_user_company from the userdata is an array like:
array( 1, 2 )


Viewing all articles
Browse latest Browse all 8245

Trending Articles