WP magic fields query_posts - wordpress

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.

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

Only display upcoming events in Wordpress Widget

Through a shortcode I placed the following code which is intented to display a random upcoming event in the sidebar. At the moment it displays a random of ALL events. How would I change the code to only display events that are younger than the current date?
<?php
// Build a custom query to get posts from future dates.
$querystr = `
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'f21_event_startdate'
AND STR_TO_DATE(wpostmeta.meta_value,'j. M Y') >= CURDATE()
AND wposts.post_status = 'publish'
AND wposts.post_type = 'events'
ORDER BY STR_TO_DATE(wpostmeta.meta_value,'j. M Y') RAND
LIMIT 1
`;
$events = $wpdb->get_results($querystr, OBJECT);
if ($events):
global $post;
foreach ($events as $post):
setup_postdata($post); ?>
<?php the_field('f21_event_startdate'); ?>
<h2><a style="color:#1e73be;" href="<?php the_permalink() ?>"><?php the_title(); ?></h2>
<div style="margin-top:10px; margin-bottom: 10px;"><?php the_post_thumbnail('full'); ?></div></a>
<b><?php the_field('f21_event_sub_header'); ?></b>
<?php endforeach;
else : ?>
<li>Sorry, no events coming up.</li>
<?php endif; ?>
you can WP_Query(). check the code below.
$args = array(
'post_type' => 'events',
'posts_per_page' => -1,
'meta_key' => 'f21_event_startdate',
'meta_value' => date('Ymd'),
'meta_compare' => '>=',
'orderby' => 'rand',
'order' => 'ASC'
);
$upcoming_events = new WP_Query( $args );
Avoid the SQL query in WordPress for getting post-type data.
we have a function for that please use
$args = array(
'post_type' => 'tribe_events',
'post_status' => 'publish',
'posts_per_page' => 10,
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => '_EventStartDate',
'meta_type' => 'DATETIME',
'paged'=>$page,
'meta_query'=>array(
array(
'key' => '_EventStartDate',
'value' => date( 'Y-m-d H:i:s', current_time( 'timestamp' ) ),
'compare' => '>'
),),
'tax_query'=>array(array(
'taxonomy' => 'tribe_events_cat',
'field' => 'id',
'terms' =>$tribe_events_cat,
))
);
$events = new WP_Query($args);
Summary: if you are using custom fields in WordPress make sure your custom fields are stored as a post meta then you can easily filter that,
In the above code, we have a meta query where we can use key, value, and compare. thanks.

Wordpress display posts from multiple value in meta

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'
)
)
);

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'
)
)
));

get custom woocommerce product

i want to get 10 product from a category in woocommerce
for example, for get latest post of a posts category i use the following code
<?php $posts = get_posts( 'category=17&numberposts=5' ); ?>
<?php if( $posts ) : ?>
<ul>
<?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
<li><i class="circle"></i><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
i want a code, like this for get woocommerce products
try this example :
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '12',
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
)
),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id', //This is optional, as it defaults to 'term_id'
'terms' => 26,
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
)
)
);
$products = new WP_Query($args);
/* your loop */
Hope this will helps you.
Try this example,
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 10,
'product_cat' => 'hoodies'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
echo '<br />' . woocommerce_get_product_thumbnail().' '.get_the_title().'';
endwhile;
wp_reset_query();
?>
OR
<?php
$args = array( 'post_type' => 'product', 'category' => 34, 'posts_per_page' => -1 );
$products = get_posts( $args );
?>
Hope this will helps you.
For more details please visit,
Woocommerce get products
why not use woocommerce's product_category shortcode?
<?php echo do_shortcode("[product_category category='17' limit='5']"); ?>
It will list the products same as in shop page. With these you are safe with product attributes like if when it's out of stock.

Resources