I'm using wordpress with custom post type UI plugin and ACF plugin.
Trying to build a “single” template with multiple feeds of custom post types by custom custom taxonomy. Using this code, with a few variations to figure out what am i doing wrong.
Got 2 pieces of code like this in a row
<?php if( get_field('collectiona') ):
$argsc = array(
'post_type' => 'products',
'product-collections' => get_field('collectiona'),
);
$prods2 = new WP_Query( $argsc );
if( $prods2->have_posts() ) {
while( $prods2->have_posts() ) {
$prods2->the_post();
?>
Whatever post code
<?php
}
}
else {
echo '';
}
?>
<?php endif; ?>
collectiona is a taxonomy field. With the piece of code, shown above, it just shows all the “products” posts out there. I’ve also tried using a text field with taxonomy slug. It shows first feed perfectly fine, if i’m not using first if statement (<?php if( get_field(‘collectiona’) ): ?>), and if that statement is present- same thing happens. All the “products” are shown. However, even with first feed shown fine, 2nd feed still shows all the “products” out there. Despite what taxonomy slug says.
I’m trying to build it the way, admin could chose a dropdown taxonomy. Text field with taxonomy slug is just an example.
p.s.
I use term object
Full template code is here jsfiddle.net/pudfbxhv . I know jsfiddle is useless for wp templates, but that's a pretty big piece of code
EDIT
Here is updated code
<?php
$taxterms = get_field("collectiona"); ?>
<?php
$args = array(
'post_type' => 'products',
'tax_query' => array(
array(
'taxonomy' => 'product-collections',
'field' => 'id',
'terms' => $taxterm->term_id
)
)
);
$myquery = new WP_Query( $args );
if($myquery->have_posts()) : ?>
<ul>
<?php while ( $myquery->have_posts() ) : $myquery->the_post(); ?>
<li> <img src="<?php the_field('prod_featured_image'); ?>" onmouseover="this.src='<?php the_field('prod_hover_featured_image'); ?>'" onmouseout="this.src='<?php the_field('prod_featured_image'); ?>'" />
<h2><?php the_field('prod_subtitle'); ?></h2>
<p>$<?php the_field('prod_price'); ?></p>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); ?>
Well, that may be a perversion, but it worked for me.
$termss = get_field('collectiona');
$slll = $termss->slug;
$args = array(
'post_type' => 'products',
'product-collections' => $slll,
);
$lineblocks = new WP_Query( $args );
if( $lineblocks->have_posts() ) {
while( $lineblocks->have_posts() ) {
$lineblocks->the_post();
Also, gotta remember to put the following code after every array
<?php wp_reset_query(); ?>
Related
I have Custom Post Types in my website, and a Custom Taxonomies inside of each.
When i use just 'post_type' in WP_Query array with a CPT name in it, it works fine, but when i want to display childrens (taxonomies) i have nothing in response.
Example structure (let's say that there are registered custom names):
Custom_Post1
|--- Custom_Tax1
|--- Custom_Tax2
Custom_Post2
|--- Custom_Tax1
|--- Custom_Tax2
Code which works GOOD:
$current_items_args = array(
'post_type' => 'Custom_Post1',
);
$current_items = new WP_Query( $current_items_args );
<?php if ($current_items->have_posts()): ?>
<?php while ($current_items->have_posts()): ?>
<?php $current_items->the_post(); ?>
<h2><?php the_title();?></h2>
<?php endwhile; ?>
<?php endif; ?>
Code which works BAD:
$current_items_args = array(
'tax_query' => array(
array(
'taxonomy' => 'Custom_Tax1',
'field' => 'slug',
),
),
);
$current_items = new WP_Query( $current_items_args );
<?php if ($current_items->have_posts()): ?>
<?php while ($current_items->have_posts()): ?>
<?php $current_items->the_post(); ?>
<h2><?php the_title();?></h2>
<?php endwhile; ?>
<?php endif; ?>
I have tryed many similar posts in stackoverflow and entire google, but can't understand where is the problem. Already spent on it all day. It is possible that i put some bad collons right here but that's not the point in my problem.
Thanks
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); ?>
I'm trying to search within a single category on wordpress using the code below
`
$args = new WP_Query( 'cat=199' );
if( $args->have_posts() ){
while ( $args->have_posts() ){ $args->the_post();
get_template_part('content', 'what-we-eat');
} ?>
<?php
}
else{
_e( 'Sorry, no posts matched your criteria.' );
}
`
however, the results shows all of the posts within that category even though the search term updates with the search term used.
Any ideas where i'm going wrong?
EDIT
I've noticed then when I remove my custom query it works, but pulls in posts from all of the categories on my blog which is what I don't want. So it's a question of excluding all other categories
Try this
<?php
global $post;
$args = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 199 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach;
?>
You want standard WP search to work in only one category?
Try to add this code to the functions.php of your theme:
function search_rules($query) {
if ($query->is_search) {
$query->set('cat','199');
}
return $query;
}
add_filter('pre_get_posts','search_rules');
I'm a bit of a wordpress newbie and was trying to find good solution what I was trying to do. Essentially I to get a list of categories that have a specific tag 'detination'. Once I do this I want to query for all posts with the above filter as well.
So What I have now is something like this:
$destCategories = get_categories( array('tag' => 'destination' , 'exclude' => '1') );
$posts = get_posts($destCategories);
However, get_categories returns an Array of category specific information which can't really be used as a filter for get_posts. Does anyone have suggestions on how can solve this? I could essentially manually iterate through the destCategories array and construct a string of all the category names and use that as a filter in get posts, but I wanted to know if there's a more elegant solution that might be available.
Thanks for your help!
There's no tag argument for get_categories() : http://codex.wordpress.org/Function_Reference/get_categories#Parameters
I guess you could try passing in a taxonomy array, I don't know how it's gonna work out though.
If you want to get posts by tag, you could just use get_posts() : http://codex.wordpress.org/Template_Tags/get_posts#Taxonomy_Parameters
You could try something like this :
$args = array(
'post_type' => 'post',
'tag' => 'destination',
);
$query = new WP_Query( $args );
if ($query->have_posts()):
while ($query->have_posts()): $query->the_post(); ?>
<?php the_title(); ?>
<?php
endwhile;
endif;
?>
Ok so i ended up with something like this, which seems to work:
$query = get_posts( $myfilter );
$destCategories = get_categories( array('tag' => 'destination' , 'exclude' => '1') );
?>
<div id="main">
<div id="content clearfix">
<p>this is using front-page.php</p>
<?php //echo var_dump($destCategories) ?>
<?php
if ( have_posts( $myfilter ) ) {
foreach ( $destCategories as $category ) :
$posts = get_posts( array('category_name' => $category->slug) );
foreach ( $posts as $post ) :
?>
<h3> <?php echo the_title(); ?> </h3>
<?php
endforeach;
wp_reset_postdata();
//the_content();
endforeach;
?>
I am creating a website that integrates a portfolio which uses custom post types, this was done based off of this tutorial.
So far it is exactly what I am looking for and works great except for one small detail. In order to fetch the posts from the new custom post type the author of the tutorial used the query_posts() codex. So the top of my portfolio page looks like this:
<?php
/* Template Name: Portfolio */
get_header();
query_posts('post_type=portfolio&posts_per_page=10');
?>
What I gather is that this declares "get posts from "post type" portfolio and show 10 per page". My problem is that I can't go get content from my portfolio page. It seems that now my portfolio page only fetches the content from the custom post type, and I can't use:
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; // end of the loop. ?>
to get content from the actual page.
This is what I am trying to do, I've replaced:
query_posts('post_type=portfolio&posts_per_page=10');
with:
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
if ( is_page( 8 ) && $query->is_main_query() )
$query->set( 'post_type', array( 'portfolio' ) );
return $query;
}
This seems like the right track, but it stills doesn't work. I'm not getting the posts from my custom post type.
Any ideas how I could modify this? I am also still learning so being clear with explanations would be greatly appreciated.
Thank you!
Editing the pre_get_posts will replace the original query and you will not have the content for your page at all. I would only recommend going this approach if you only wanted to display the content of your portfolio post type and not the content of your portfolio page.
For general post queries it is recommended to use WP_Query or get_posts.
http://codex.wordpress.org/Class_Reference/WP_Query
http://codex.wordpress.org/Template_Tags/get_posts
If you use the WP_Query function the wp_reset_postdata() will restore the post data back to the original so you can get the content of your original page.
$args = array(
'posts_per_page' => 10,
'post_type' => 'portfolio',
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Now you will be able to use the original loop to show the content of your page
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; // end of the loop. ?>
Usually, I stick my query posts in a variable, like so:
$catid = get_cat_ID('My Category Name');
$args = array(
'posts_per_page' => 5,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'category' => $catid
);
$posts_array = get_posts($args);
Then you can loop it like so:
<?php foreach ($posts_array as $post) : setup_postdata($post);?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<?php endforeach; ?>
Finally, to access your page content you can use the variable $post, it's automatically set by wordpress. No need to add any more code than this to access your page content.
<?php foreach( $posts as $post ) : setup_postdata($post); ?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<?php endforeach; ?>
The foreach loop for your page content is a little overkill, and there is a better way to do it (most likely at least), but I haven't been bothered to look into it further yet! It works though!