Philip Seyfi
Philip Seyfi

Reputation: 969

Wordpress: Skip posts in the_loop or query with array meta_keys

Each of my posts has a meta_key which is an array with user IDs. Is there a way of displaying only posts whose array meta_key contains a specific user ID?

I haven't find any way of applying such a filter in the query, and if I skip posts inside the loop, the function doesn't show enough posts.

Upvotes: 0

Views: 997

Answers (3)

Rochester Oliveira
Rochester Oliveira

Reputation: 1483

You could try create a user_meta that stores the posts that he liked, só you just explode this array and show all this posts in a loop ( could be using include parameter ).

Upvotes: 0

user518505
user518505

Reputation: 73

Did you try something like this?

$temp_store_query = $wp_query;
$wp_query = NULL;
$quer_y = 'meta_key=your_meta_key&meta_value=some_user_ID';
$wp_query = new WP_Query( $quer_y );

while( $wp_query->have_posts() ) : $wp_query->the_post();
// ...do whatever is needed here
endwhile; 

$wp_query = NULL; 
$wp_query = $temp_store_query;

Upvotes: 0

Dominic
Dominic

Reputation: 2457

You could skip posts inside the loop as you're doing, but run your query an ALL posts, and just stop/break the loop once you've reached the number of posts you want to display.

get_posts('numberposts=-1') removes the limit — http://codex.wordpress.org/Template_Tags/get_posts This kind of thing is likely to break paging however so it depends how you'd use it.

You could also try the $meta_key and $meta_value get_posts parameter but it's probably only going to work for you if you have each user ID as an individual value in it's own custom field.

Like so:

Key: UserID, Value: 3

Key: UserID, Value: 5

Key: UserID, Value: 7

Key: UserID, Value: 8

It sounds like something in which Wordpress' built-in user roles might be better. Perhaps explain what your goal is?

Upvotes: 1

Related Questions