Store post meta key into an array - php

I want to be able to retrieve the _octopus_id postmeta and store that into the ids array, what am I doing wrong?
$ids = [];
var_dump($ids);
$args = array(
'post_type' => 'offices',
'post_status' => 'any',
'meta_query' => array(
array(
'key' => '_octopus_id',
'value' => $ids,
),
)
);
$the_query = new WP_Query($args);
Update: I've got the offices stored in an array, but I can't figure out how to get the post meta key _octopus_id only out of the office and store that within ids.
/* Set the WP offices in an array */
$ids = new WP_Query([
'post_type' => 'office',
'post_status' => 'any',
'posts_per_page' => 10,
'meta_query' => array(
array(
'key' => '_octopus_id',
),
)
]);
$posts[] = $ids->posts;
var_dump($posts);
var_dump(is_array($posts));

You first need to get the content with the query and then loop it and fill the array. What you do, is trying to fetch the content by the value which is null in your case.

Related

woocommerce, product link by reviews loop

I created the very simple loop here, the aim is to extract only reviews that have a value equal to or greater than 4 stars. And so far so good. Now from this loop I would also like to get the link to the specific product. Anyone have any idea how to get this information?
$reviews = get_comments( array(
'status' => 'approve',
'post_status' => 'publish',
'post_type' => 'product',
'meta_query' => array( array(
'key' => 'rating',
'value' => array('4','5'),
) ),
) );
foreach( $reviews as $review) {
/* stuff */
}
Working on it a little longer, I gave myself the answer, I attach the solution to my problem:
<?php
$reviews = get_comments( array(
'status' => 'approve',
'post_status' => 'publish',
'post_type' => 'product',
'meta_query' => array( array(
'key' => 'rating',
'value' => array('4','5'),
) ),
) );
foreach( $reviews as $review) {
$postId = $review->comment_post_ID;
the_permalink($postId);
}
?>

How to query posts by ACF field but then also order by a separate ACF field?

I am using WP_Query to get posts that have a specific value in one of the ACF fields. I also need to order them by a separate ACF field. I am not sure how to accomplish this. Everything I've read says to use 'orderby' => 'meta_value' but I believe thats the value of the field I'm filtering the posts by, which is not what I want.
This is what I have right now..
$args = array(
'post_type' => 'contacts',
'posts_per_page' => -1,
'meta_key' => 'department',
'meta_value' => 'Transportation',
'orderby' => 'meta_value'
);
$the_query = new WP_Query( $args );
I need to orderby an ACF field named last_name.
It's possible to assign a name to a meta query, and then refer to that name in your orderby. Something like this.
$args = array (
'post_type' => 'contacts',
'post_status' => 'publish',
'nopaging' => true,
'posts_per_page' => -1,
'meta_query' => array( 'main_query' => array(
'key' => 'department',
'value' => 'Transportation'
), 'orderby_query' => array(
'key' => 'last_name',
)
),
'orderby' => array(
'orderby_query' => 'ASC',
),
);
$the_query = new WP_Query( $args );

Wordpress orderby meta values in array

I'm trying to get sorted list of posts using get_posts by meta value and the order of meta value is given in array.
This is what I currently have.
$stores = get_posts(array(
'post_type' => 'stores',
'post_status' => 'publish',
'posts_per_page' => -1,
'fields' => 'ids', // I only need the ID's of posts
'orderby' => 'meta_value',
'meta_key' => 'state',
'order' => 'ASC'
));
This returns the array of posts sorted by meta_value in ASCENDING alphabetical order.
I have an array of possible values for 'meta_key' => 'state', i.e. array('State1', 'State2', 'State3')
I want to set order so that all stores which has meta value State1 appears first, then from State2 and after that State3
I can't use order by numeric value and alphabetical value as state names are gonna be random.
I found one post here, it is using mera_query_orderby. I can't find any documentation for this and tried it, but it's not working. It returns posts ordered by ID.
Any help would be appreciated. Thanks
EDIT:
I added the meta_query_orderby filters in functions.php
And the updated code I used from EXAMPLE 2, is like:
$stores = get_posts(array(
'post_type' => 'stores',
'post_status' => 'publish',
'posts_per_page' => -1,
'fields' => 'ids',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'state', // Custom field key.
'value' => array("CState1", "AState2", "BState3")
)
),
'meta_query_orderby' => array(
array(
'key' => 'state', // (required) Custom field key.
'value' => array("CState1", "AState2", "BState3")
)
)
));
I have an array of possible values for 'meta_key' => 'state', i.e.
array('State1', 'State2', 'State3')
If you want to sort the posts by the meta value in the exact order as in the above array, you can use a custom WP_Query parameter (to set the meta/sort values) and the posts_orderby filter to customize the ORDER BY clause, and in that clause, you would be using the FIELD() function in MySQL.
Step 1
Add this code to your plugin or theme (if theme, you'd add the code to the theme functions file):
add_filter( 'posts_orderby', 'posts_orderby_meta_value_list', 10, 2 );
function posts_orderby_meta_value_list( $orderby, $query ) {
$key = 'meta_value_list';
if ( $key === $query->get( 'orderby' ) &&
( $list = $query->get( $key ) ) ) {
global $wpdb;
$list = "'" . implode( wp_parse_list( $list ), "', '" ) . "'";
return "FIELD( $wpdb->postmeta.meta_value, $list )";
}
return $orderby;
}
Step 2
When making your post queries, set the orderby to meta_value_list and add meta_value_list to the query parameters — if you're using get_posts(), make sure suppress_filters is set to false:
$stores = get_posts( array(
'post_type' => 'stores',
'post_status' => 'publish',
'posts_per_page' => -1,
'fields' => 'ids', // get just the ID's of posts
'meta_key' => 'state',
'orderby' => 'meta_value_list',
'meta_value_list' => array( 'State1', 'State2', 'State3', '' ),
'suppress_filters' => false,
) );
PS: I the added '' to the array so that posts where the metadata is ('') (i.e. exists in the database, but the value is empty) would be placed at the bottom of the results.
Tried and tested working, but note that the above solution is only for single orderby, which means array is not supported.
/*Display posts of type 'stores', ordered by 'state', and filtered to show only states.*/
$args = array(
'post_type' => 'stores',
'meta_key' => 'state',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'state',
'value' => array( 'State1', 'State2', 'State3'),
'compare' => 'IN',
),
),
);
$query = new WP_Query( $args );

Ordering Wordpress posts by multiple meta values

Using get_posts(), I need to first retrieve posts that fall on a certain day (the day is set by a custom field - just the date, not time). I do this by using a meta key/value. Then, I need to order these posts based on the time of day (which is a separate custom field, just time, not date). So essentially I need to pull in all the events that fall on a given day, and order them according to the time.
First I grab the day, using a custom field:
if ( get_field('festival_day') ) {
$day_stamp = get_field('festival_day');
}
Then I set my arguments for the query:
$args = array(
'posts_per_page' => -1,
'post_type' => 'event',
'meta_key' => 'event_date',
'meta_value' => $day_stamp
);
$events = get_posts( $args );
So.. the question is, how do I query the other custom field (which is the start time), and then sort by that time? The time field key is event_start_time.
Thanks!
You can user WP_Query to retrieve your events and you can query it something like below:
$args = array(
'post_type' => 'event',
'order' => 'DESC',
'orderby' => 'meta_value',
'meta_key' => 'event_start_time',
'meta_query' => array(
array(
'key' => 'event_start_time',
'value' => 'yourValue_here',
'compare' => '>='
),
array(
'key' => 'event_date',
'value' => $day_stamp,
'compare' => '='
)
)
);
$query = new WP_Query( $args );
UNTESTED but it should work.
Check this code, it should work to sort your post according to time from resulting post of day.
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'meta_key' => 'event_start_time',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query'=> array(
array(
'key' => 'event_date',
'value' => $day_stamp,
'compare' => 'IN'
)
)
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>'. get_the_title().'</li>';
}
echo '</ul>';
} else {
// no posts found
}

Sort post meta data using meta_key?

I'm trying to get the post, meta datas on table wp_postmeta. I need to get the post id, meta keys and meta value by using the meta_key and post id. The meta key stored is date. eg,. 2014-01-02, 2014-02-03, I have to query them based on the year or month.
$args = array(
'post_type' => 'calendar_holiday',
'ID' => $id;
'meta_query' => array(
array(
'key' => ,
'value' => ,
'compare' => 'LIKE'
),
)
);
$query = new WP_Query( $args );
I'm not sure what to put on key and value.. any idea?? thanks
// 'orderby' => 'meta_value_num', // it's a numeric value
$args = array(
'orderby' => 'meta_value',
'post_type' => 'calendar_holiday',
'meta_query' => array(
array(
'key' => 'over-make',
'value' => 'Doral',
'compare' => 'LIKE'
)
)
);
$query = new WP_Query( $args );

Categories