I would create a custom loop in the home page that will display only the posts that contains a specific word in its custom field.I'l try to explain it with an example.
EX. I got a website of a Basketball team and 2 custom post types called "COACh" and "TRAINING".
In "COACH" i add the coach and its skills.
In "TRAINING" i add the exrcise's name,the description and in the custom meta box i add the time,the coach,the duration and the day of the execution.
Now,suppose that it's Monday i would display all the TRAININGS that contains the day Monday in its custom field.
Is it possible without a plugin like event calendar????
IS IT RIGHT?
$today = date("l");
$args = array( 'post_type' => 'palinsesto', 'posts_per_page' => -1);
$palinsesto = get_posts( $args );
foreach ( $training as $post ) {
$day = get_post_meta( $post->ID, 'giorno ' ); // here day is key
if($day==$today)
while ($post->have_posts()) : $post->the_post(); ?>
Yes it is possible, I coded below roughly:
$args = array( 'post_type' => 'TRAINING', 'posts_per_page' => -1);
$training = get_posts( $args );
foreach ( $training as $post ) {
$day = get_post_meta( $post->ID, 'day ' ); // here day is key
if($day=='Monday')
echo $post->post_title.'<br />';
echo $post->post_content.'<br />';
}
this one is the solution for my question...it works great
$today = strftime('%A');
$day = get_post_meta( $post->ID, 'giorno ' );
$my_query = new WP_Query(array(
'post_type' => 'palinsesto',
'meta_query' => array(
array(
'key' => 'giorno',
'meta-value' => $value,
'value' => $today,
'compare' => '=',
))));
if (have_posts()) :
while ($my_query->have_posts()) : $my_query->the_post(); ?>
Related
On my Wordpress website users can write their own posts on the group wall. I want to display all published posts on the group wall. I also want to display pending posts to the current user who is the author of these posts. Right now when the user submits a new post, there is no information displayed about it, they may not know if their post got submitted. I want to show them their pending post with the information that they need to wait for the post review.
Is there a way to add the logic to the $args of the WP_Query? This is my code right now:
//number of posts per page default
$num =5;
//page number
$paged = $_POST['page'] + 1;
$current_user = wp_get_current_user();
$uid = $current_user->ID;
//args
$meta_query = array();
$meta_query[] = array('key' => 'pm_accessible_groups','value' => sprintf(':"%s";',1),'compare' => 'like');
$args = array(
'post_type' => 'pg_groupwalls',
'post_status' => 'publish',
'posts_per_page' =>$num,
'meta_query' => $meta_query,
'paged'=>$paged
);
//query
$query = new WP_Query($args);
//check
if ($query->have_posts()):
//loop
while ($query->have_posts()): $query->the_post();
And later I have my template. This way it is only showing published posts as of now. I tried to find some information about it here: https://developer.wordpress.org/reference/classes/wp_query/ , and later I tried changing the $args array to something like this:
//number of posts per page default
$num =5;
//page number
$paged = $_POST['page'] + 1;
$current_user = wp_get_current_user();
$uid = $current_user->ID;
//args
$meta_query = array();
$meta_query[] = array('key' => 'pm_accessible_groups','value' => sprintf(':"%s";',1),'compare' => 'like');
$args = array(
'post_type' => 'pg_groupwalls',
'post_status' => array (
'relation' => 'AND',
array(
'post_status' => 'publish',
),
array(
'post_status' => 'pending',
'author' => $uid,
),
),
'posts_per_page' =>$num,
'meta_query' => $meta_query,
'paged'=>$paged
);
//query
$query = new WP_Query($args);
//check
if ($query->have_posts()):
//loop
while ($query->have_posts()): $query->the_post();
It is most probably not the way to do this, and obviously, it does not work the way I want. It shows both published and pending posts to all users (not just the current user who is an author). I could maybe create a new WP_Query, but this one is already paginated and showing 5 posts per page with a "Load More" button, I don't want to break the pagination.
Is there a way to create this logic using $args array? Or any other way to achieve what I want?
Thank you in advance.
If there is more information needed or more of the code, I will try to update it.
Try below code.
//number of posts per page default
$num = 5;
//page number
$paged = $_POST['page'] + 1;
$current_user = wp_get_current_user();
$uid = $current_user->ID;
//args
$meta_query = array();
$meta_query[] = array(
'key' => 'pm_accessible_groups',
'value' => sprintf(':"%s";',1),
'compare' => 'like'
);
For all publish post.
$publish_args = array(
'post_type' => 'pg_groupwalls',
'post_status' => array( 'publish' ),
'posts_per_page' => $num,
'meta_query' => $meta_query,
'paged' => $paged
);
//query
$publish_posts = new WP_Query( $publish_args );
//check
if ( $publish_posts->have_posts() ):
//loop
while ($publish_posts->have_posts()): $publish_posts->the_post();
endwhile;
wp_reset_postdata();
endif;
For all pending posts of current user.
$pending_args = array(
'post_type' => 'pg_groupwalls',
'post_status' => array( 'pending' ),
'post_author' => $uid,
'posts_per_page' => $num,
'meta_query' => $meta_query,
'paged' => $paged
);
//query
$pending_posts = new WP_Query( $pending_args );
//check
if ( $pending_posts->have_posts() ):
//loop
while ($pending_posts->have_posts()): $pending_posts->the_post();
endwhile;
wp_reset_postdata();
endif;
I'm using polylang plugin to having multi-languages website.
Using WooCommerce with polylang demands duplicating each product for each language, so assuming I have Hebrew and English, that means 2 duplications for each products.
It works fine with WooCommerce plugin, but when I'm displaying "related products" at the end of the product page, it's mixing products in English and Hebrew together.
I expect to filter the related product by website current language (if(get_locale() == 'en_US') - to check website current locale state, else will represent Hebrew).
Polylang functions
Here is what I have tried, but I got stuck on the part of filtering the product by language:
add_filter( 'woocommerce_product_related_posts', 'custom_related_products' );
function custom_related_products($product){
global $woocommerce;
// Meta query
$meta_query = array();
$meta_query[] = $woocommerce->query->visibility_meta_query();
$meta_query[] = $woocommerce->query->stock_status_meta_query();
$meta_query = array_filter( $meta_query );
// Get the posts
$related_posts = get_posts( array(
'orderby' => 'rand',
'posts_per_page' => '4',
'post_type' => 'product',
'fields' => 'ids',
'meta_query' => $meta_query
) );
if ( $related_posts->have_posts() ) {
while ( $related_posts->have_posts() ) : $related_posts->the_post();
if(pll_get_post_language(get_the_ID())){
//Not sure its the right approach for this..
}
endwhile;
}
$related_posts = array_diff( $related_posts, array( $product->id ), $product->get_upsells() );
return $related_posts;
}
How can I filter Woocommerce related product section by language?
Edit
So after a little bit of research and help in the comments I found out that 'lang' => 'en' argument does exist, but even when I use it, there is no change on related products language display.
Any ideas?
add_filter( 'woocommerce_product_related_posts', 'custom_related_products' );
function custom_related_products($product){
global $woocommerce;
// Meta query
$meta_query = array();
$meta_query[] = $woocommerce->query->visibility_meta_query();
$meta_query[] = $woocommerce->query->stock_status_meta_query();
$meta_query = array_filter( $meta_query );
// Get the posts
$related_posts = get_posts( array(
'orderby' => 'rand',
'posts_per_page' => '4',
'post_type' => 'product',
'fields' => 'ids',
'meta_query' => $meta_query,
'suppress_filters' => false
) );
if ( $related_posts->have_posts() ) {
while ( $related_posts->have_posts() ) : $related_posts->the_post();
if(pll_get_post_language(get_the_ID())){
//Not sure its the right approach for this..
}
endwhile;
}
$related_posts = array_diff( $related_posts, array( $product->id ), $product->get_upsells() );
return $related_posts;
}
suppress_filters There is a way to make get_posts cache the results however, the suppress_filters option is true by default, but if you set it to false, the caching mechanisms inside WordPress will do their work, and results will be saved for later.
You can try this code:
$related_posts = get_posts( array(
'orderby' => 'rand',
'posts_per_page' => '4',
'post_type' => 'product',
'meta_query' => $meta_query,
'lang' => 'en'
) );
if ($related_posts) {
foreach ($related_posts as $post) {
setup_postdata($post);
// something like <li><?php the_title(); ?></li>
}
}
wp_reset_postdata();
This code correctly returns code in selected language on my site
While working on a custom WordPress REST Api end point to fetch post by selected language or device language, this worked. See if it can help you out.
function mycustomplugin_posts($params) {
// get the url params
$page = $params->get_param('page') ? $params->get_param('page') : 0;
$per_page = $params->get_param('per_page') ? $params->get_param('per_page') : 10;
$offset = $params->get_param('offset') ? $params->get_param('offset') : 0;
$order = $params->get_param('order') ? $params->get_param('order') : 'desc';
$order_by = $params->get_param('order_by') ? $params->get_param('order_by') : 'date';
$lang = array_search($params->get_param('lang'),polylang_json_api_languages(), true) ? $params->get_param('lang') : pll_default_language();
$args = [
'post_type' => 'post',
'page' => $page,
'numberposts' => $per_page,
'offset' => $offset,
'order' => $order,
'orderby' => $order_by,
'tax_query' => [
[
'taxonomy' => 'language',
'field' => 'slug',
'terms' => $lang
]
]
];
$posts = get_posts($args);
$data = [];
$i = 0;
foreach($posts as $post) {
// Extract the post data
$data[$i]['id'] = $post->ID;
$data[$i]['title'] = $post->post_title;
$data[$i]['content'] = $post->post_content;
$data[$i]['excerpt'] = e42_the_short_content($post->post_content, 300);
$data[$i]['slug'] = $post->post_name;
$data[$i]['date'] = $post->post_date;
$data[$i]['link'] = get_permalink($post->ID);
$data[$i]['author'] = get_the_author_meta('display_name', $post->post_author);
$data[$i]['categories'] = array();
$data[$i]['featured_image']['thumbnail'] = get_the_post_thumbnail_url($post->ID, 'thumbnail');
$data[$i]['featured_image']['medium'] = get_the_post_thumbnail_url($post->ID, 'medium');
$data[$i]['featured_image']['large'] = get_the_post_thumbnail_url($post->ID, 'large');
foreach(get_the_category($post->ID) as $category){
array_push($data[$i]['categories'],$category->term_id);
}
$i++;
}
// Create the response object
$response = new WP_REST_Response( $data );
// Add a custom status code
$response->set_status( 200 );
return $response;
}
/plugins/woocommerce/templates/single-product/related.php
Move this to child theme
yourtheme/woocommerce/single-product/related.php
And add the following line to the foreach loop
if (pll_current_language()!=pll_get_post_language($post_object->ID)) {
continue;
}
Basically if current language does not match product language we skip over it.
Same as title: I need make Wordpress loop that will display posts sorted by comments date.
Like on forum, first is post with the latest comment.
It is interesting. There might be two approached which can resolve this.
1 -
$args = array(
'status' => 'approve',
'order' => 'DESC'
);
$comments = get_comments( $args );
$ids = array();
foreach ( $comments as $comment ) {
$post = get_post( $comment->comment_post_ID );
if(!in_array($post->ID, $ids):
$ids[] = $post->ID;
echo $post->title;
endif;
}
2 -
Add a action to add a meta to the post for the recent comment date.
add_action('comment_unapproved_to_approved', 'wpc_2511_comment_approved');
function wpc_2511_comment_approved($comment) {
$comment_post_ID = $comment->comment_post_ID;
$date = $comment->comment_date;
update_post_meta( $comment_post_ID, '_recent_comment_date', $date );
}
Then write the query string to get the posts sorted by meta_key & meta_value Descending
Like:
$args = array(
'post_status' => 'publish',
'post_type' => 'post'
'meta_key' => '_recent_comment_date',
'orderby' => 'meta_value_num',
'order' => 'DESC'
);
Hope this will help you :)
I have some 30 orders in my shop. I am trying to loop over all orders, but I can't retrieve any orders. Here's the code:
$args = array (
'post_type' => 'shop_order',
'posts_per_page' => - 1
);
$loop = new WP_Query($args);
while ($loop->have_posts()) {
// do some work here
}
The loop never runs. I tried printing a count of all post types:
$args = array (
'post_type' => 'any',
'posts_per_page' => - 1
);
$loop = new WP_Query($args);
$types = array();
while ($loop->have_posts()) {
$loop->the_post();
$post_id = get_the_ID();
$type = get_post_type($post_id);
if ($types[$type]) $types[$type]++;
else $types[$type] = 1;
}
foreach ($types as $type => $count) {
echo "{$type}: {$count} ";
}
This is printing product: 30 page: 5 post: 1, i.e. no shop_orders. I guess I'm missing something very obvious, but it's not so obvious to me what that obvious thing is!
UPDATE: I am now retrieving all orders with this code:
$args = array(
'post_type' => 'shop_order',
'posts_per_page' => -1
);
$posts = get_posts($args);
That doesn't answer the question though. But it's a solution.
use 'post_status' => 'wc-processing' or 'post_status' => 'any'
I'm having a little trouble with a custom taxonomy template. I inherited a site that was developed by someone else and they use "Types" plugin to add some custom taxonomies.
Goal:
to have an archive template that shows only posts with a certain taxonomy term in it at example-domain.com/people/harrison-ford
Problem:
This code is bringing in posts that do not have the taxonomy selected.
Here's my full code:
<?php
$year = get_post_meta($post->ID, 'year', true);
$post_type = 'post';
$tax = 'people';
$tax_terms = get_terms( $tax );
if ($tax_terms) {
$args = array(
'post_type' => $post_type,
'people' => 'harrison-ford',
"$tax" => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1,
'orderby' => 'date',
'order' => DESC
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) : ?>
<h2 class="wwNews"><?php echo $tax_term->name; ?> News</h2>
<?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<-- display stuff -->
<?php endwhile; // end of loop ?>
<?php endif; // if have_posts()
wp_reset_query();
}
?>
What are you expecting here? "$tax" is going to to be 'people' =>, which is going to overwrite 'harrison-ford' to the value of $tax_term->slug.
'people' => 'harrison-ford',
"$tax" => $tax_term->slug,
Furthermore, I don't know of any custom argument called people, I'm pretty sure that you want tax_query:
'tax_query' => array(
'taxonomy' => 'people',
'terms' => array('harrison-ford', $tax_term->slug)
)
Which will give you the results of all people matching harrison-ford and the value of $tax_term->slug within the taxonomy of people