Custom Post with Products - php

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

Related

Display content from two different custom post types

I want to loop through two different custom post types to find out what should be displayed. In CPT project I want to now the name of the house, in CPT portfolio I want to know the category. If these two are the same, I want a certain content from the project CPT to be shown.
On this page you're on the single-portfolio.php.
The code below have two errors (at least) the first one is that all the projects house types ($houseType) displays for no reason and the second one is that the content within the second loop doesn't display at all.
So, to try to make this a bit more clear:
I want to display the name and the city of the project which house type name matches the portfolio category (which is the name of the house type)
$args = array('post_type' => 'project');
$my_query = new WP_Query( $args );
if( $my_query->have_posts()) while ($my_query->have_posts()) : $my_query->the_post();
$houseType = the_field('hustyp');
$argsPort = array(
'post_type' => 'portfolio',
'tax_query' => array(
array(
'taxonomy' => 'portfolio_category',
'field' => 'slug', //can be set to ID
'terms' => $houseType
)));
$port_query = new WP_Query($argsPort);
if( $port_query->have_posts()) while ($port_query->have_posts()) : $port_query->the_post(); ?>
<tr>
<td><?php the_field('brf')?>/<?php the_field('ort')?></td>
</tr>
<?php
endwhile;
?>
<?php
endwhile;
I don't know if the best way to go is by creating an own query or something like that.
Any ideas?
UPDATE
Okay, I made some progress and finally realized that the_field displays the field and therefor I should use get_field. But I still don't know how to compare the two different post types to see if it is a match, and after that display only the matches. I'm thinking that creating an array can be a solution, but don't know how to go forward. As you can see in my new code below.
New code:
$args = array(
'post_type' => array( 'portfolio', 'project' ));
$my_query = new WP_Query( $args );
if( $my_query->have_posts()) {
while ($my_query->have_posts()) : $my_query->the_post();
//Returns All Term Items for "my_taxonomy"
$term_list_portfolio = wp_get_post_terms($post->ID, 'portfolio_category', array("fields" => "all"));
// Just get the category
$category = $term_list[0]->name;
// Get the house type
$houseType = get_field('hustyp');
$result = array($term_list[0]->name, get_field('hustyp'));
print_r($result);
if($category == $houseType) { ?>
<tr>
<td><?php the_field('brf')?><?php the_field('ort') ?>
<?php }
endwhile;
}
Try to reset query <?php wp_reset_query(); ?> before second wp_query reffer

Advanced Custom Fields Relationship - Basic loop + reversed?

I want to connect companies with products using Advanced Customs Fields. These are some pages on my website:
www.example.com/companies/apple/
www.example.com/companies/microsoft/
www.example.com/products/iphones/
www.example.com/products/ipods/
For example I want to connect Apple with their products (iPhones and iPods).
I have added a relationship field in ACF that is called related_articles that is only available when the pages parent is the /companies/-page.
So on Apple's company-page - the products iPhones and iPods have been selected in the custom relationship field related_articles.
On page.php I added the following:
<?php
$posts = get_field('related_articles');
if( $posts ): ?>
<?php foreach( $posts as $p ): // variable must NOT be called $post (IMPORTANT) ?>
<?php echo get_the_title( $p->ID ); ?><br>
<?php endforeach; ?>
<?php endif; ?>
On the page www.example.com/companies/apple/the following is returned:
iPhones<br>
iPods<br>
Which works exactly as I want.
The problem is that http://www.example.com/products/iphones/ and http:// www.example.com/products/ipods/ is empty. Here I want the relationship to be reversed and Apple<br> to be displayed. How can I solve this? Is it possible with a code that is compatible in both ways?
I do not wish to make a second "connection", from products to the companies - this needs to be handeled from a single page and not two. Custom post types is also a no go.
Updated - New code from the Querying relationship fields documentation:
<?php
$related_articles = get_posts(array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'related_articles', // name of custom field
'value' => '"' . get_the_ID() . '"',
'compare' => 'LIKE'
)
)
));
?>
<?php if( $related_articles ): ?>
<?php foreach( $related_articles as $article ): ?>
<?php echo get_the_title( $article->ID ); ?><br>
<?php endforeach; ?>
<?php endif; ?>
This dosen't return anything (blank).
This is certainly possible - ACF allows for 2 way or reverse querying of related items. So, your product page, you could do the following (with your own relevant acf names)
global $post;
$related_company = get_posts(
array(
'post_type'=>'company',
'meta_query'=>array(
array(
'key' => 'related_product',
'value' => '"'.$post->ID.'"',
'compare' => 'LIKE'
)
)
));
The whole process is explained in detail on the ACF site: http://www.advancedcustomfields.com/resources/querying-relationship-fields/
Although, whether this is possible without using Custom Post Types, I'm not certain.

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

Custom Post Type displays post belong on the category

I have a custom post type advice and taxonomy adcat. I want to display all post belong to that category.
Lets say I have 4 categories namely : 'games', 'tours', 'dishes', 'hotels' and also this four category is a menu. If I click one of the category for example: hotels all post belong to the hotels should display.
By the way this code I used to show wordpress default categories:
<?php $catname = wp_title('', false); ?>
<?php $posts = get_posts("category_name=$catname&numberposts=8&offset=0");
foreach ($posts as $post) : start_wp(); ?>
//html output
<h1><?php the_title(); ?></h1>
<?php endforeach; ?>
this is not working in custom post 'taxonomies' any suggestion would be helpful thank's
try this maybe it work... I write a note so you can see whats going on.. hope it help
<?php
// Get the term/category of the post
$terms = get_the_terms( $post->ID , 'advice-cat' );
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, 'advice cat' );
}
//WordPress loop for custom post type
$terms = get_the_terms( $post->ID , 'advice-cat' );
$my_query = new WP_Query('post_type=advice&advice-cat=' . $term->name . '&posts_per_page=-1');
while ($my_query->have_posts()) : $my_query->the_post(); ?>
// output content
<?php the_title(); ?>
<?php endwhile; wp_reset_query(); ?>
try something like this, this is the example to fetch post by category id
$args = array( 'cat' => $cat_id, 'post_type' => 'advice', 'posts_per_page' => -1 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
endwhile;
wp_reset_postdata();
you can also use with category name as well.
$args = array('category_name' => 'catname', 'post_type' => 'advice', 'posts_per_page' => -1 );
you can use post_per_page as per your requirement, I have just added -1 for all the posts
I don't really understand your terminology. When you are talking about adcat, is it a custom taxonomy or a term of the build-in taxonomy category. If adcat is a custom taxonomy, you should use the tax_query in WP_Query,not the category parameters.
Remember, category and a custom taxonomy are both taxonomies, their direct children is called terms, and their direct childen is called child terms
You should also not be using wp_title() to get the queries object. You should be using get_query_var() to get the queried object. For categories it will be cat, taxonomy will be taxonomy and for terms term. Have a look at get_categories, get_taxonomies and get_terms for returned values
Example
$category = get_query_var( `cat` );
$cat_slug = $category->slug;
You can now return $cat_slug to your custom query as category_name
EDIT
I've quickly rethinked the whole thing and checked your comment as well. Why not just copy your index.php and rename it taxonomy.php. There is no need at all for a custom query here. The default loop in taxonomy.php should do it
EDIT 2
For further reading, go and check the following articles
Theme Development
Template Hierarchy
Not sure if I read your answer correctly but what I am assuming is you want to search posts by the taxonomy terms correct? So in your adcat taxonomy you have 'games', 'tours', 'dishes', 'hotels' as your terms or categories. Your best option would be to use a tax_query in your query arguments. Here is how to do that with the WP_Query() object.
<?php
$args = array(
'post_type' => 'advice',
'posts_per_page' => 8,
'tax_query' => array(
'taxonomy' => 'adcat',
'terms' => array('games', 'tours', 'dishes', 'hotels'),
'field' => 'slug'
)
);
$query = new WP_Query($args);
if($query->have_posts()) : while($query->have_posts()) : $query->the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php endwhile; endif; ?>
<?php wp_reset_postdata(); ?>
Important Note:
When using a WP_Query you will be overwriting the default post data. So in order to get that data back you just use the wp_reset_postdata() as you can see in the example above after the loop has finished.

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