Horje
wp query with multiple of same meta keys - Wordpress Solution
I have a wp_query and the posts will have multiple of the same meta keys; I'm looking for an easy way to not show posts that contain a meta key with the ID of the user. The issue is this does not work I still get posts that have a meta key with the users id in it. Basically they have already voted don't show them the post anymore.
$args = array(
                'post_type'      => 'posts',
                'posts_per_page' => -1,
                'post_status'    => array('publish'),
                'meta_query'     => array(
               array(
                 'key' => 'boxes',
                 'value'   => array('22','23'),
                 'compare' => 'IN',
               ),
		array(
                 'key' => 'operations_type',
                 'compare' => 'NOT EXISTS',
               ),
	      array(
                 'key' => 'votes',
                 'value' => $id,
                 'compare' => '!=',
               ),
              ),
             );


Solution - 1

Hello there, if you store the "votes" key as an array you must edit the code to be


$args = array(
'post_type' => 'posts',
'posts_per_page' => -1,
'post_status' => array('publish'),
'meta_query' => array(
'relation' => 'AND',  /* You can use  AND for all conditions  /  Use OR if any condition is true */
array(
'key' => 'boxes',
'value' => array('22','23'),
'compare' => 'IN',
),
array(
'key' => 'operations_type',
'compare' => 'NOT EXISTS',
),
array(
'key' => 'votes',
'value' => $id,
'compare' => 'NOT IN',
),
),
);


Solution - 2

Hello! I have a feeling that the metafield "votes" would be an array containing the ids of all the users that voted the post, isn't it? If that is the case it should be 'compare' => 'NOT IN' another thing maybe try to specify the parameter "relation" on top of the metaquery, like: 'meta_query' => array( 'relation' => 'AND', array( ...


Solution - 3

You may need to get the values of the votes into an array.


//get all values regardless of the key
$all_values = get_post_meta(get_the_ID(), '', true);

//you could do print_r($all_values) to see how the array is structured. It will most likely be like this:
// //Array ( [votes] => Array ( [0] => value_1 ), [another_key] => Array ( [0] => value_2 ) )

//based on this array, you can now loop 
$votes_array = $all_values['votes'];
and then you can use the compare as "NOT IN"


Solution - 4

If you want a query that return result from multiple meta keys


$args = array(
'post_type' => 'posts',
'posts_per_page' => -1,
'post_status' => array('publish'),
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'boxes',
'value' => array('22','23'),
'compare' => 'IN',
),
array(
'key' => 'operations_type',
'compare' => 'NOT EXISTS',
),
array(
'key' => 'votes',
'value' => $id,
'compare' => 'NOT IN',
),
),
);
I hope it would be helpful





Wordpress

Related
list of the children of a Woocommerce Grouped product ( with the Product picture,name and price ) - Wordpress Solution list of the children of a Woocommerce Grouped product ( with the Product picture,name and price ) - Wordpress Solution
Create array of hours within wordpress loop - Wordpress Solution Create array of hours within wordpress loop - Wordpress Solution
Query Parameters Errors - Wordpress Solution Query Parameters Errors - Wordpress Solution
I need to speed up my wordpress-site - Wordpress Solution I need to speed up my wordpress-site - Wordpress Solution
I want to create a search form that will only search a custom post typ - Wordpress Solution I want to create a search form that will only search a custom post typ - Wordpress Solution

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
26