Wordpress display posts from multiple value in meta - wordpress

I have got main product with meta key: addons and meta values in this key: 129456,968945,495435 Each of these three numbers is the key with these meta values. For example:
Post 1: meta_key: subproduct meta_value: 129456
Post 2: meta_key: subproduct meta_value: 968945
Post 3: meta_key: subproduct meta_value: 495435
And now I want to display these three posts in the main product. My code:
<?php if (!empty($addons = get_post_meta(get_the_ID(), 'addons', true))):?>
<?php
$params = array(
'post_type' => 'product',
'meta_key' => 'subproduct',
'meta_value' => $addons
);
$wc_query = new WP_Query($params);
?>
<?php while ($wc_query->have_posts()) : $wc_query->the_post(); ?>
<?php include(rh_locate_template('inc/parts/woomain.php')); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif;?>
With one meta value it worked but with several it doesn't work anymore. How do you view these three posts?

Try to alter your query like this and lets see if that works
$params = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'subproduct',
'value' => array($addons),
'compare' => 'IN'
)
)
);

It works with this code:
$params = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'subproduct',
'value' => $addons,
'compare' => 'IN'
)
)
);

Related

woocommerce product shortcodes template

As I know
[products limit=”4″ columns=”4″ on_sale=”true” ] will show all on sale products,
[products limit=”3″ columns=”3″ best_selling=”true” ] will show best selling products,
If I were not wrong, both of the shortcodes would call the content-product.php file to display the products.
My question is how can I make different product shortcodes call different php template files to show the related products instead of calling the same file that is content-product.php?
For example,
on sale shortcodes call content-product-1.php
best selling shortcodes call content-product-2.php
Thank you!
Create our own shortcode to display the specific products and our our own template as below,
for example, wc_get_template_part( 'content', 'product-test' );
add_shortcode( 'sale_products_test', 'sale_products' );
function sale_products( $atts ){
global $woocommerce_loop, $woocommerce;
extract( shortcode_atts( array(
'per_page' => '12',
'columns' => '3',
'orderby' => 'title',
'category' => 'clearance',
'order' => 'asc'
), $atts ) );
// Get products on sale
$product_ids_on_sale = woocommerce_get_product_ids_on_sale();
$meta_query = array();
$meta_query[] = $woocommerce->query->visibility_meta_query();
$meta_query[] = $woocommerce->query->stock_status_meta_query();
$args = array(
'posts_per_page'=> $per_page,
'orderby' => $orderby,
'order' => $order,
'no_found_rows' => 1,
'post_status' => 'publish',
'post_type' => 'product',
'orderby' => 'date',
'order' => 'ASC',
'meta_query' => $meta_query,
'post__in' => $product_ids_on_sale,
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $category,
)),
);
ob_start();
$products = new WP_Query( $args );
$woocommerce_loop['columns'] = $columns;
if ( $products->have_posts() ) : ?>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php wc_get_template_part( 'content', 'product-test' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
<?php endif;
wp_reset_postdata();
return ob_get_clean();
}
Not Possible. This type of shortcode require to use wp_query and fetch data your requirement wise

Select post with specific group - subfield value

in wordpress I have custom post type 'referenzen'. This post type has Custom fields(ACF) 'Referenzen-buildin-type' Group with subfield 'building-type' which is checkbox. I do not know how to select posts with specific building type. This is not working for me :
$posts = get_posts(array(
'meta_query' => array(
array(
'key' => 'referenzen-building-types_building-type',
'value' => '"Museen"',
'compare' => 'LIKE'
)
)
));
Any idea? Thanks
Please have a look below:
<?php
//args
$args = = array (
'numberposts' => -1,
'post_type' => 'event',
'meta_key' => 'location',
'meta_value' => 'Melbourne'
);
//query
$the_query = new WP_Query( $args );?>
<?php if( $the_query->have_posts() ): ?>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
/* do something */
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
I hope it would help you out.
$posts = get_posts(array(
'meta_query' => array(
array(
'key' => 'referenzen_building_types_building_type',
'value' => 'Museen',
'compare' => 'LIKE'
)
)
));

Show categories not in gallery post format

I want to show all blog categories that aren't of type gallery. My code almost works but if I have 2 posts in the same category the category is shown twice ie:
If I create a category called 'news' and add 2 non-gallery posts, it shows up as:
news
news
instead of just
news
<?php
$galleryPosts = new WP_Query(array(
'post_type' => 'post',
'order' => 'ASC'
));
?>
<?php if ( $galleryPosts->have_posts() ) : ?>
<?php while ( $galleryPosts->have_posts() ) : $galleryPosts->the_post(); ?>
<?php if(!has_post_format('gallery')) {
the_category();
}
?>
<?php endwhile; ?>
<?php endif; ?>
To suppress non-gallery posts, try selecting only those posts initially and then remove the has_post_format('gallery') check:
$posts = new WP_Query(
array(
'post_type' => 'post',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array(
'post-format-gallery'
),
'operator' => 'NOT IN'
)
)
)
);
Then in PHP:
if ($posts->have_posts()) {
while ($posts->have_posts()) {
$posts->the_post();
the_category();
}
}

WP magic fields query_posts

I'm trying to display titles from posts of type 'products' with that have a custom field "product_category" (dropdown) with value "food".
When the "food" field is set to type "text" everything is fine. When I change the type of this
field to "dropdown" nothing appears.
To create custom page and custom fields I use Magic Fields plugin in Wordpress.
Here's the code:
<?php
global $wp_query;
query_posts(array(
'post_type' => 'products',
'meta_query' => array(
array(
'key' => 'product_category',
'value' => 'food',
)
))
);
while(have_posts()) : the_post(); ?>
<?php $key = get_post_meta($post->ID, 'title'); ?>
<li <?php post_class(); ?>><?php if($key) { echo $key[0]; } else { the_title(); }; ?></li>
<?php
endwhile;
wp_reset_query();
?>
use meta_key and meta_value
<?php
global $wp_query;
query_posts(array(
'post_type' => 'products',
'meta_query' => array(
array(
'meta_key' => 'product_category',
'meta_value' => 'food',
)
))
);
?>
You need to add the parameter metacompare to the array (meta_compare => 'like') because on table wp_postmeta the key_value is saved like "a:1:{i:0;s:7:"Food";}"
so
you should change to:
query_posts(array(
'post_type' => 'products',
'meta_key' => 'product_category',
'meta_value' => 'food',
'meta_compare' => 'like'
))
);
Try using meta_query instead. Make sure you try both products and product as your post_type parameter.
$args = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'product_category',
'value' => 'food'
)
)
);
$query = new WP_Query( $args );
Also, don't use query_posts. Here's why.

Wordpress custom field date, display post with custom date > current in ascending order

How do I order post by a custom field if "releasedate" > "currentdate" but in acending order. Basically only display post with dates starting after today but in ascending order. currently i have
<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('showposts=10&post_type=movies&meta_key=releasedate_value&orderby=releasedate_value&order=ASC');
if (have_posts()) : while (have_posts()) : the_post();
$currentdate = date("Y-m-d",mktime(0,0,0,date("m"),date("d"),date("Y")));
?>
<?php if ($releasedate > $currentdate) {?>
"my contents/ post here"
<?php } ?>
<?php endwhile; ?>
<?php endif; ?>
now everything works except when its ASC and not DSC, no post will display because wordpress in getting the post first and the erasing the post that are not before current date and only 10 post are allowed, therefore if 10 post has a release date before today, wordpress loads then erase them after and leaves everything blank! Thank you for your help
I'd put the date criteria into the query itself. Assuming you have WordPress version 3.1 or higher, you can use the meta_query parameter. Something like:
<?php
$currentdate = date("Y-m-d",mktime(0,0,0,date("m"),date("d"),date("Y")));
$wp_query = new WP_Query( array ('showposts' => 10,
'post_type' => 'movies',
'meta_query'=> array(
array(
'key' => 'releasedate_value',
'compare' => '>',
'value' => $currentdate,
'type' => 'DATE',
)),
'meta_key' => 'releasedate_value',
'orderby' => 'meta_value',
'order' => 'ASC'
)
);
should work.
This works with me too:
$currentdate = date("Y-m-d",mktime(0,0,0,date("m"),date("d"),date("Y")));
$args = array(
'post_type' => 'event',
'meta_key' => 'date_field',
'posts_per_page' => 5,
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'date_field',
'value' => $currentdate,
'compare' => '>=',
'type' => 'DATE'
),
),
);
$my_query = null;
$my_query = new WP_Query($args);

Resources