Loop with non-repeated fields - php

I just started using advanced custom fields and it is awesome. I have been struggling with this one issues and I would love some help with it.
I have setup a product name, price and image field under the products field group. I have it working so it shows the information on the product page but I have no clue how to do the loop for the overview of all products (i.e. I want to make a page that has all the products on it).
<?php if(get_field('products')): ?>
<?php the_sub_field('product-name');?>
<?php the_sub_field('product-price');?>
<?php the_sub_field('product-image');?>
<?php endif; ?>
Is anyone on the interweb able to help me out and give me a quick lesson on advanced custom fields looping? I would appreciate it very much.

I'm assuming this is a field for a custom post type?
If so, you just need to do a WordPress loop.
<?php
// Loop through custom post type
$loop = new WP_Query( array( 'post_type' => 'YOURPOSTTYPE', 'posts_per_page' => -1, 'orderby' => 'menu_order', 'order' => 'ASC' ) );
while ( $loop->have_posts() ) : $loop->the_post();
if(get_field('products')) :
the_sub_field('product-name');
the_sub_field('product-price');
the_sub_field('product-image');
endif;
endwhile;
?>

Related

Custom Post with Products

i am trying to get all details of my custom post and print selected data from that on my single-product page.
so lets say the custom post is named Games and the product page is the one were you select on of the games to buy.
i am using:
$args = array(
'post_type' => 'games',
)
);
$review_details = new WP_Query($args);
this is getting me most of the information the product instance, although i chose the post type to be games. since in the post i have the age and rating for the games.
How will I be able to get all the details i already have in the Games Post to the single-product page of a game i am selling?
$args = array(
'post_type' => 'reviews',
'meta_query' => array(
array(
'key' => 'games_books', // name of custom field
'value' => '"' . $game_id . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
)
));
$review_details = new WP_Query($args);
print_r($review_details);
and when i use print_r($review_details); all the parameters of reviews are empty, but when i do the same from a post then i can get the reviews.
again i need to print this data on the single-product page
It sounds like you have additional meta information for the Custom Post Type 'games'. Outputting this extra information can be done using either:
get_post_meta($post_id, 'key');
See here for more information https://developer.wordpress.org/reference/functions/get_post_meta/
Or if you're using a common custom field plugin, like ACF, you'll need to use get_field()
get_field('key', $post_id);
See here for more info https://www.advancedcustomfields.com/resources/get_field/
To print a data for custom post, you can use WP_Query with While Option.
For example:
<?php
$args = array( 'post_type' => 'games');
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<!-- Loop Start -->
<?php the_title(); ?>
<!-- Loop End -->
<?php else: endif; ?>
If you want to print custom areas
<?php echo get_post_meta($post->ID, 'key', true); ?>

Writing a loop to conditionally display a custom post type, checking for ACF fields and Event Calendar Pro events

I'm making a site for a cookery school, and I need help with writing a loop. The loop will display the types of classes they provide, as long as each of those types of classes has upcoming events. Any help would be really appreciated, as everything I've tried so far has failed.
I'm using the Advanced Custom Fields plugin as well as the Events Calendar Pro plugin from Modern Tribe. I'll refer to the post type created by the Events Calendar Pro plugin as 'events' from now on.
I have a custom post type called 'class-type' which I've set up in my functions.php file. Each post of this type is a type of class that the cookery school offers, for instance DIY Pizza Club, Street Food Mastercless etc. The single post view will have images and details of that class, with a loop of upcoming events of that category, with the categories being the class name.
The 'class-type' post type has an ACF field called 'class_age', which is a select menu with the options 'For Adults' or 'For Kids & Teens'. It also has an ACF field called 'class_to_display', which is a taxonomy field showing categories from the Tribe Events posts.
The cookery school has two locations, and each event has it's location set to one of these.
The loop I need help with will have 4 variations on four different pages, and once I have the loop working I can make these variations myself. The variations will be:
Bristol, For Adults
Bristol, For Kids & Teens
Cardiff, For Adults
Cardiff, For Kids & Teens
So using the first variation as an example, the loop will need to do the following:
Get posts of type 'class-type'.
For each post, check that the post's ACF field 'class_age' is 'For Adults'.
Get the value in the post's ACF field 'class_to_display' and store it in a variable.
Check for posts of type 'events' with that variable as their category and with 'Bristol' as their location.
If events exist that meet those criteria, display the 'class-type' post's ACF fields of 'class_main_image', 'class_title', and 'class_short_description'.
I hope that's clear, and feel free to ask if you have any questions. Again, I really appreciate anyone taking the time to help!
Here's what I have so far which goes up to step 3 above, and is tested and working (the echo in p tags is just to check it works):
<?php
$args = array(
'post_type' => 'class-type',
'meta_key' => 'class_age',
'meta_value' => 'For Adults'
);
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ):
while ( $the_query->have_posts() ) : $the_query->the_post();
$cookery_class = get_field('class_to_display'); ?>
<p><?php echo $cookery_class->name; ?></p>
<?php endwhile;
endif;
wp_reset_query(); ?>
Much to my surprise, I nailed it! Here's the code that works, hope it helps somebody.
<?php
$args = array(
'post_type' => 'class-type',
'meta_key' => 'class_age',
'meta_value' => 'For Adults'
);
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ):
while ( $the_query->have_posts() ) : $the_query->the_post();
$cookery_class = get_field('class_to_display');
$cookery_class = (str_replace(' ', '-', strtolower($cookery_class->name)));
$check_has_events = tribe_get_events( array(
'tribe_events_cat' => $cookery_class,
'venue' => 59
)
);
if($check_has_events) {
$image = get_field('class_main_image'); ?>
<div class="cookery-class">
<?php if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
<h3><?php the_title(); ?></h3>
<p><?php the_field('class_short_description'); ?></p>
</div>
<?php }
endwhile;
endif;
wp_reset_query(); ?>

How to fetch all the woocommerce products from the database using php

I am trying to fetch all the products of the woocommerce from the database using php.. But it is not giving me the list of all products
<select Name='choose'>
<?php
$args = array( 'post_type' => 'product' ,'posts_per_page' => 100);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) :
$loop->the_post();
echo '<option selected value="'.the_title().'</option>';
endwhile;
?>
</select>
can anybody please tell me how can I get all the products of woo commerce
Thanks
In order to fetch Single product to your single.php page you can make use of the code which i have already suggested over here. Refer here:
https://wordpress.stackexchange.com/questions/240653/how-to-query-single-product-in-woocommerce/240726#240726
In order to fetch All products that you have into your DB you can follow the steps below.
You can fetch the Woo-commerce Products based on the WP_Query also so that it is also saved in the wp_posts table alone. So that we might slightly modify the post type alone for getting the WP_Query to work on.
Step One:
First We shall see to the parameters what we have to pass for the WP_Query.
$params = array(
'posts_per_page' => 5,
'post_type' => 'product'
);
Note: As you can see, all we’ve done is added a post_type variable to the array, and set it’s value to "product"; the query will now look for WooCommerce products instead of posts.
Step Two: After that we have to run on with the same structure that the Normal WP_Query behaves.
<?php
$params = array(
'posts_per_page' => 5,
'post_type' => 'product'
);
$wc_query = new WP_Query($params);
?>
<?php if ($wc_query->have_posts()) : ?>
<?php while ($wc_query->have_posts()) : $wc_query->the_post(); ?>
<?php the_title(); //Prints the title over Here ?>
<?php //You can add what ever you need from the loop over here ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p>
<?php _e( 'No Products' ); ?>
</p>
<?php endif; ?>
Explanations
An array of parameters for WP_Query to work with is created; to start with it’s just the basic posts, but by adding more specifics to these parameters we can achieve different results for our queries.
WP_Query is used to query the parameters created in the first line.
The query is checked to see if it returned any results.
If there are results, then we iterate over them:
First, setting the global variable $post, which ensures that the next function works.
Second, setting the the_title() variable, which is responsible for displaying the product title.
Then, when the posts are displayed, we return the $post variable to its original state with thewp_reset_postdata function.
Using this link i have studied how to query the WooCommerce Products using WP_Query and I have shared here so that all might get used with this posts.
Happy Coding :)
Your code is working fine on my machine, However its seems some conflict to your previous wp query. You can also try to get all product like this..
<select Name='choose'>
<?php
$args = array( 'post_type' => 'product' ,'posts_per_page' => 100);
$products = get_posts( $args );
foreach( $products as $product ) :
echo '<option selected value="'.$product->post_title.'</option>';
endforeach;
?>
</select>
$this->db->select('*');
$this->db->from('vm_posts');
$this->db->join('vm_postmeta', 'vm_postmeta.post_id = vm_posts.id');
$this->db->join('vm_wc_product_meta_lookup', 'vm_wc_product_meta_lookup.product_id = vm_postmeta.post_id');
$this->db->join('vm_inventory', 'vm_wc_product_meta_lookup.sku = vm_inventory.product_sku');
$this->db->join('vm_terms', 'vm_inventory.customer_id = vm_terms.name');
$this->db->where('post_type','product');
$query = $this->db->get()->result();

Display post as featured if taxanomy matches current category

I have used ACF. I have added the 'taxonomy' select dropdown field to the post edit page. From the dropdown I select which category this post should be featured in, but my code is displaying the same post as featured across all categories.
Below is the code in the category.php file. I need it to display the most recent post which has been given a 'Feature In Category', and to therefore be featured in the category I have defined.
My current loop in category.php
<?php
$category = get_field('feature_in_category');
// args
$args = array(
'numberposts' => -1,
'posts_per_page' => 1,
'category__in' => $category,
'orderby'=> 'modified'
);
// get results
$the_query = new WP_Query( $args );
// The Loop
?>
<?php if( $the_query->have_posts() ): ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="small-6 columns">
<div class="img-box-shadow">
<a href="<?php the_permalink(); ?>">
<?php echo the_post_thumbnail(); ?>
</a>
</div>
</div>
<div class="small-6 columns">
<h3><?php echo the_title(); ?></h3>
<p><?php echo the_excerpt(); ?></p>
</div>
<?php endwhile; ?>
<?php else : echo '<p style="color:#fff;">no posts</p>'; ?>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
Pastebin: http://pastebin.com/NR3UanAd
I'm not very familiar with ACF and haven't yet used it. I had a look at some documentation etc, and this is what I've found:
ACF TAXONOMY FIELD
The ACF taxonomy field will return NULL in your application as the ID being passed to get_field is a category ID, which is a non valid post ID. Yes, you can pass a category ID to get_field, but then there must be a custom field assigned to that particular category in order to return the value of that custom field.
Look at the ACF docs
$field = get_field($field_name, $post_id, $format_value);
◦$post_id: Specific post ID where your value was entered. Defaults to current post ID (not required). This can also be options / taxonomies / users / etc
ACCESS STORED DATA BY ACF
As I stated before, I'm not familiar with ACF but it seems that ACF field data is stored in the same way as data is stored by the default custom fields. So you will have a matching pair of key=>value sets. This data can be accessed and retrieved by a meta_query
THE IDEA
The ACF Taxonomy dropdown saves a selected value which translated into a category. Again, I'm not sure whether it saves the name, slug or ID, but from what I can pick up, it saves the category ID, so I will make my assumptions on that
So, the first thing to do here is, get the currently viewed category's ID. This can can be retrieved by get_queried_object_id()
Now that you have the current category ID, you will need to match that to a value for the specific meta_key=feature_in_category and return the posts that has this specififc key=>value pair attached to it
THE CODE
Your code should look something like this with the assumption ACF data is stored in the same way as data is stored from a custom field (This code requires PHP5.4+, for earlier versions, change [] to array())
$cat_id = get_queried_object_id(); // Assumption that category ID is saved by ACF
$args = [
'posts_per_page' => 1,
'orderby' => 'modified',
'meta_key' => 'feature_in_category',
'meta_value_num' => $cat_id, // Assumption that category ID is saved by ACF
];
$q = new WP_Query( $args );
if( $q->have_posts() ) {
while( $q->have_posts() ) {
$q->the_post();
// YOUR TEMPLATE TAGS AND MARKUP
}
wp_reset_postdata();
}
If the category ID is not saved by the ACF field, and it saves the name or slug, you can change the code as follows
$category = get_queried_object()->name; // For category name
or
$category = get_queried_object()->slug; // For category slug
Then in your query arguments, change
'meta_value_num' => $cat_id, // Assumption that category ID is saved by ACF
to
'meta_value' => $category,
EDIT
In addition and posted as a comment, the query aruments as used by the OP, working.
$cat_id = get_queried_object_id();
$args = [
'posts_per_page' => 1,
'orderby' => 'modified',
'meta_query' => [
[
'key' => 'feature_in_category',
'value' => $cat_id,
'compare' => 'IN'
]
]
];
After reviewing the documentation here http://codex.wordpress.org/Class_Reference/WP_Query it appears that your code is doing all the right stuff. I believe your answer lies in the actual query string that is being used to produce your results. You should be able to see that query string in the properties of the query object by doing:
<?php
$category = get_field('feature_in_category');
// args
$args = array(
'numberposts' => -1,
'posts_per_page' => 1,
'category__in' => $category,
'orderby'=> 'modified'
);
// get results
$the_query = new WP_Query( $args );
echo '<pre>'; print_r($the_query); die();
I suggest running the query string once you see it directly on the database using something like phpMyAdmin or Navicat to connect directly to the database. You should be able to adjust your query there to produce your desired results and then adjust your arguments accordingly. It may also be that there is nothing wrong with your query and that the issue lies with the category functionality instead. This debugging method should reveal that as well if that is the case.
UPDATE:
I think you just need to use 'cat' and not 'category__in'. Also, make sure you use what you set for Field Name inside of the call to get_field. This feedback is based on this example: http://www.the-perfect-life.org/html-5-css-3-php-wordpress-jquery-javascript-photoshop-illustrator-tutorial/how-to-create-custom/wordpress-plugin-theme-codex-code-function/advanced-custom-fields-get-field-taxonomy-query-related-posts-by-tag-and-category/
$categoryValue = get_field('feature_in_category');
$args=array(
'cat' => $categoryValue,
'posts_per_page'=>1 // Number of related posts to display
);
$my_query = new wp_query( $args );
while( $my_query->have_posts() ) {
$my_query->the_post();
?>
<div>
<a href="<? the_permalink()?>">
<?php the_title(); ?>
</a>
</div>
wp_reset_query();

Fetch and display Wordpress category of custom post type

I am currently working on a personal project and the this page basically has two tabs each will display the archive for specific categories under one custom post type called webinar.
I am calling the category in one of the tabs using
<?php query_posts('category_name=demos-on-demand-videos'); ?>
However when i do this i' just getting the no post's found screen, what am i doing wrong? I am trying to display the post archive from the category demos-on-demand-videos which is under the webinar custom post type.
use this
query_posts( array( 'post_type' => 'webinar','your-custom-taxnomy' => 'demos-on-demand-videos' ) );
while ( have_posts() ) :
the_post();
$post_id = $post->ID;
endwhile;
Follow this link:
http://eyan16.wordpress.com/2013/09/16/how-to-fetch-posts-from-custom-posts-type-with-custom-taxonomy/
Make a page template for each of your tabs and use this custom loop in it. Make sure to adjust it for your specific post type, taxonomy, or term.
<?php $args=array(
'post_type' => 'webinar', //set the post_type to use.
'taxonomy' => 'demos-on-demand-videos', // set the taxonomy to use.
'term' => 'term1', //set which term to use or comment out if not using.
'posts_per_page' => 10 // how many posts or comment out for all.
);
$webinarloop = new WP_Query($args);
if($webinarloop->have_posts()) : while($webinarloop->have_posts()) :
$webinarloop->the_post();
get_template_part( 'content' ); //or whatever method you use for displaying your content.
endwhile; endif; //end the custom post_type loop
?>
This code works for me just fine
* x is taxonomy name name you have created
* y is category slug
$args = array('post_type' => 'client','posts_per_page'=>'8','order'=>'DESC','x'=>'y');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();

Categories