Need a query where I can get products_ids from orders with the condition that such order must contain a specific product_id.
With this, what I want to get is other products bought by other users when chosen a specific product.
This is the query I'm working now:
$orders = new WP_Query(
array(
'post_type' => array('shop_order'),
'post_status' => array('wc-completed', 'wc-pending')
)
);
but do not know how to insert the conditionals.
Related
I'm using WordPress meta data to register clicks on images, to know which images each user has clicked - and also the total number of clicked images per user. The first part is fine, but I'm struggling to get the counter going, as it's returning a lower amount of meta data than what is actually there.
I have a custom post type gallerier and each gallery has a number of images. I'm using the meta key nedlasting, and I'm identifying each image individually by fetching the url.
Here is how I register clicks, after checking it isn't already:
// Add meta query if it doesnt already exist
function sjekk_nedlasting( $postid, $url, $dato) {
$brukerid = (string)get_current_user_id();
// Check if the image is downloaded previously
$args = array(
'post_type' => 'gallerier',
'meta_query' => array(
array(
'key' => 'nedlasting',
'value' => sprintf(':"%s";', $url),
'compare' => 'LIKE'
),
array(
'key' => 'nedlasting',
'value' => sprintf(':"%s";', $brukerid),
'compare' => 'LIKE'
)
),
'fields' => 'ids'
);
// Perform the query
$nedl_query = new WP_Query( $args );
$nedl_ids = $nedl_query->posts;
// If not already downloaded, register it
if ( empty( $nedl_ids ) ) {
$metaarray = Array(
'user_id' => $brukerid,
'url' => $url,
'date' => $dato
);
add_post_meta( $postid, 'nedlasting', $metaarray );
}
}
Then I'm trying to count those registered clicks using the following function:
// Count number of downloads for a single user
function tell_nedlastinger() {
$brukerid = (string)get_current_user_id();
$args = array(
'post_type' => 'gallerier',
'meta_query' => array(
array(
'key' => 'nedlasting',
'value' => sprintf(':"%s";', $brukerid),
'compare' => 'LIKE'
)
),
'fields' => 'ids'
);
// perform the query
$nedl_query = new WP_Query( $args );
$nedl_ids = $nedl_query->posts;
return count($nedl_ids);
}
The function returns a number, but always much lower than the actual amount of registered meta data/clicks. Anyone seeing a problem?
Edit: I'm pretty sure the problem is that I'm getting the total number of posts, not the total number of meta data entries/clicks - which more often that not is several per post. Any way around that?
I never found a solution to query meta data and get x results per post, so instead of using an array in a single key I split the post meta data into three keys. Then I used $wpdb for a custom query. The final code for the tell_nedlastinger() function:
$brukerid = (string)get_current_user_id();
global $wpdb;
$query = $wpdb->get_results("SELECT * FROM wp_postmeta WHERE (meta_key = 'nedlasting_brukerid' AND meta_value = '$brukerid')");
return count($query);
You can improve the speed of this by using:
$brukerid = (string)get_current_user_id();
global $wpdb;
$count = $wpdb->get_row("SELECT COUNT(*) AS THE_COUNT FROM $wpdb->postmeta WHERE (meta_key = 'nedlasting_brukerid' AND meta_value = '$brukerid')");
return $count->THE_COUNT;
The speed is improved by only having to fetch one row from the database. Also note the use of:
FROM $wpdb->postmeta
This allows for a variable table prefix instead of assuming a table wp_ prefix.
$metaCount = count( get_comment_meta($postId,'key','value') );
I'm trying to get a list of all posts from a custom post type called social, and any normal posts within the category social. I currently am using the following:
$posts = get_posts(
array(
'post_type' => array('social', 'post'),
'category_name' => 'social',
'post_status' => 'publish'
)
);
This seems to only be returning posts within the social category, not the social post type. Anyway to return both post_types?
The generated query expects all the conditions to be true: post_typeto be either social or post AND category_name to be social AND post_status to be publish.
One simple solution is to use WP_Query instead:
$query = new WP_Query(array(
'post_type' => array('social', 'post'),
'category_name' => 'social',
'post_status' => 'publish'
));
$posts = $query->posts;
Then, you may use a posts_where filter to modify the where part of the query:
add_filter( 'posts_where', 'my_posts_where', 10, 2 );
function my_posts_where( $where, $query ) {
...modify $where...
return $where;
}
I'll skip the string manipulation part, because there is nothing specific there. Anyhow, $where will look something like this (tested with my WordPress installation):
"AND ( wp_term_relationships.term_taxonomy_id IN (<number>))
AND wp_posts.post_type IN ('social', 'post') AND
((wp_posts.post_status = 'publish'))"
You need to convert it to something like this:
"AND ( ((wp_term_relationships.term_taxonomy_id IN (<number>)
AND wp_posts.post_type = 'post') OR (wp_posts.post_type = 'social'))
AND (wp_posts.post_status = 'publish'))"
Finally, you need to make sure that my_posts_where does not break the other queries. I might just add some indicator to the $query object to identify this particular query in the filter.
I have defined several custom post types (CPTs) with some Custom Fields (CF) and need to have a certain sorting - first by category, then by date, something along the lines of
$args = array (
'meta_value category' => 'ASC',
'meta_value startdate' => 'ASC'
),
'post_type' => 'training'
);
$the_query = new WP_Query($args);
However, this does not seem to work and the dates are not correctly parsed, since the default parsing of a meta_value is alphabetical. I cannot set the meta_type to DATE, because there are two meta_values.
Any ideas?
So, I was unable to find a solution. Instead I wrote a custom function in php that took the database result and sorted it to my needs.
This is a Thesis theme site using the core WP search functionality. Not looking for a new plugin solution, just how to alter the search results to change order of posts from relevance to chronological order. Any help is greatly appreciated.
Some of native wp search results, like terms do not have any type time info stored. You can use WP_User_Query with order of records. It will be not exactly what you expected but will have chronological order:
$args = array(
'search' => 'search_patern_from_form',
'orderby' => 'id',
'order' => 'DESC'
);
$user_query = new WP_User_Query( $args );
See order by section of WP_User_Query codex page
I have a custom post type called activities and a number of standard pages that are different countries. Each country has a number of activities that are available. The user needs to add a range of different activities, and then choose what country each activity is available in.
Using list pages, I have a dropdown listing all pages in the activities custom post type. The user is able add a new activity, add the content and select what country(page) this activity relates to. On that particular country page on the front-end the available activities are listed.
For each country I only want to show 3 activities. I do the standard loop getting all custom post types for activities. I have to check whether the postid of the page chosen in the drop down menu matches the current page, and if it does show the activity. Using the standard posts_per_page isn't working, as it only grabs three posts, then does the conditional statement on those to see if any matches the current id.
I guess what I want to happen is to have the posts_per_page only apply to activities that actually match the criteria of having the country page id matching the id of the country selected in the activity.
global $post;
$postid = get_the_ID();
$args = array( 'post_type' => 'activities', 'orderby' => 'date', 'order' =>'ASC','posts_per_page' => 3);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$country = rwmb_meta( 'rw_activities_pages' );
// this is the drop down list of pages. It gets the ID of the country page chosen
$currentID = get_post($country);
$currentTitle = $currentID->post_title;
if ($country == $postid){
// if the activity country id matches the current page show the info
echo get_the_title();
echo $currentTitle;
echo the_content();
}
endwhile;
wp_reset_query();
Any ideas would be fantastic, as I have a couple of custom post types that do a similar thing!
Thanks in advance,
Rich
What you want to do is filter the posts in the query itself using meta_query.
$args = array(
'post_type' => 'activities',
'meta_query' => array(
'key' => 'rw_activities_page',
'value'=> $postid,
'compare'=>'='
),
'orderby' => 'date',
'order' =>'ASC',
'posts_per_page' => 3);
You might need to adjust the meta_key if that's not exactly the value it's using.