I don't know how to explain my problem but I like to echo the specific category/ slug inside the specified section (sorting off by categories). I don't know which Wordpress function should be use? is it get_category or get_post?
so far I have this code to echo all the categories in one section:
<?php
$args = array(
'post_type' => 'allproducts',
'posts_per_page' => 9,
);
$posts = get_posts( $args );
foreach ( $posts as $post ):
setup_postdata( $post );
?>
basically my goal is to get all the post from specific category:
I have these list of categories with specific slugs and ID:
Category Name | Slug | ID
Shoes | cat01| 2
Pants | cat02| 3
Tops | cat03| 4
$categories = get_categories( array(
'orderby' => 'name',
) );
foreach($categories as $category)
{
$cateid = $category->term_id;
$args = array(
'post_type' => 'allproducts',
'category' => $cateid,
'posts_per_page' => 9,
);
$posts = get_posts( $args );
foreach ( $posts as $post ){
// here rest of code of posts.
}
}
I figure it out. here's my code:
<?php
$args1 = array( 'posts_per_page' => 9, 'offset'=> 0, 'category' => array(1));
$myposts1 = get_posts( $args1 );
foreach ( $myposts1 as $post ) : setup_postdata( $post );
?>
array (1) --> is the id of my category
closed it with this code
<?php endforeach;
wp_reset_postdata();?>
Related
I've made a custom post type "product" on my Wordpress site. The detail page of a product is single-product.php which shows everything about the product perfectly.
All the product will be categorized in the following structure:
Toegangscontroles
Elektronische sloten
Wandlezers
Software
...
Overige producten
Sleutelkaarten
Kluizen
...
I have two test products on my website. Both products have the category "Electronische sloten". Which is a child category of "Toegangscontroles".
I want to show related products on single-product.php This related product cannot be the current product itself and must be under the same parent category. So in this case, a product with child category "Toegangscontroles" must show 5 random related products from the child categories from parent "Toegangscontroles".
This is my code now:
<?php
$related = get_posts( array(
'post_type' => 'product',
'category__in' => wp_get_post_categories($post->ID),
'numberposts' => 5,
'post__not_in' => array($post->ID) ) );
if( $related ) foreach( $related as $post ) {
setup_postdata($post); ?>
<?php the_title(); ?>
<?php }
wp_reset_postdata();
?>
When I go the product A, I see product B under related products, but when I go the the product B page, I don't see product A. Tough they have exactly the same category.
Thanks in advance.
Haven't tested this, but you can try
$related = get_posts( array(
'post_type' => 'product',
'tax_query' => array( array(
'taxonomy' => $taxonomy_name,
'field' => 'term_id',
'terms' => wp_get_post_terms($post->ID, $taxonomy_name, array('fields' => 'ids'))
) ),
'numberposts' => 5,
'exclude' => array($post->ID)
) );
Please use below code i think it will work.
$related = new WP_Query(
array(
'category__in' => wp_get_post_categories( $post->ID ),
'posts_per_page' => 5,
'post__not_in' => array( $post->ID )
)
);
if( $related->have_posts() ) {
while( $related->have_posts() ) {
$related->the_post(); ?>
<?php the_title(); ?>
<?php }
wp_reset_postdata();
} ?>
filter category post wise on same page in WordPress if i click category
plastic i want result plastic product
for example product category
1.plastic
2.metallic
3.silver
showing image
enter image description here
post div
<?php
global $post;
$myposts = get_posts(
array(
'post_type' => 'product',
'numberposts' => '999',
'orderby' => 'menu_order',
'order' => 'ASC'
)
);
?>
sidebar div
<?php
global $post;
$curVal = "";
$myposts = get_posts(
array(
'post_type' => 'product',
'numberposts' => '999',
'orderby' => 'product_category',
'order' => 'ASC'
)
);
?>
<ul>
<?php
foreach($myposts as $post){
if($curVal != get_field('franchise_category')) { ?>
<li>
<a href="<?php echo home_url( $wp->request ); ?>?cat=<?php echo get_field('product_category'); ?>">
<?php echo get_field('product_category'); ?>
</a>
</li>
<?php }$curVal = get_field('product_category');} ?>
</ul>
1st if you want to get all posts with this query use:
'numberposts' => -1,// this will return all available, right now you are passing a string 999 - '999'
2nd - you're not setting up your post data and restoring context of the template tags Try this:
foreach ( $myposts as $post ) : setup_postdata( $post );
...
...
endforeach;
wp_reset_postdata();
Edit your code and see the result.
I have this code:
<?php
global $post;
$args = array( 'numberposts' => 4, 'category' => 15 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
The issue is that I want to grab more than just category number 15. Simply add them next to 15 like 15, 18 or "15,18" does not work.
Any ideas?
Thanks
Try this one: it is work in my project.
<?php
global $post;
$args = array( 'numberposts' => 4, 'category' => '15,8');
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
Thanks.
I've been searching high and low for and answer to this, but I'm not actually sure it's possible!
I have a WP_Query that pulls posts from almost everything, however, I wish to exclude a specific category and/or all it's sub categories.
Searching around people are yet to find a solution for this.
Here's my query so far:
$args = array(
'post_type' => 'sell_media_item',
'cat' => -98,
'orderby' => 'desc',
'paged' => $paged,
'posts_per_page' => 20
); ?>
<?php $loop = new WP_Query( $args ); ?>
I thought just excluding cat 98 would grab all the sub categories too, but apparently not.
I've tried using:
category__not_in, depth=0, parent=0 and even an adaptation of this, with no luck.
Any ideas?
[EDIT]
I'm using a custom taxonomy called Collections, so putting 'collection' => 'vip' into the query means it will only show this collection. I'm thinking if there's a way of reversing this so it excludes the collection instead?
As it's not possible to list all of the categories that will appear here as they will be changing all of the time.
[EDIT 2]
After the discussion in the comments below, here's the updated code.
$ex = array(
'taxonomy' => 'collection',
'child_of' => 98,
'hide_empty' => 0
);
$categories = get_categories($ex);
$categoriesToExclude = array();
foreach ($categories as $category) {
$categoriesToExclude[] = $category->cat_ID;
}
echo('<pre>'); var_dump($categories);
$args = array(
'post_type' => 'sell_media_item',
'category__not_in' => $categoriesToExclude,
'orderby' => 'desc',
'paged' => $paged,
'posts_per_page' => 20
); ?>
<?php echo('<br /><pre>'); var_dump($args); ?>
<?php $loop = new WP_Query( $args ); ?>
I would get the list of all sub categories with get_categories() and then build a 'cat' exclusion array based on the results.
$args = array('parent' => 98);
$categories = get_categories($args);
$categoriesToExclude = array();
foreach ($categories as $category) {
$categoriesToExclude[] = $category->cat_ID;
}
$args = array(
'post_type' => 'sell_media_item',
'category__not_in' => $categoriesToExclude,
'orderby' => 'desc',
'paged' => $paged,
'posts_per_page' => 20
); ?>
<?php $loop = new WP_Query( $args ); ?>
This is just an example, you may have to modify it slightly to fit your needs.
So!
It appears I was trying to do the impossible. I couldn't get this script working for the life of me. So I tried a different angle. Instead of excluding a custom taxonomy and its terms, I decided to move all of my other terms into a parent term and just called that instead.
Here's the code if anyone's interested...
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'sell_media_item',
'taxonomy' => 'collection',
'term' => 'clubs',
'orderby' => 'desc',
'paged' => $paged,
'posts_per_page' => 20
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) : while ($loop->have_posts()) : $loop->the_post(); ?>
I wrote my own function in order to exclude subcategory posts from the loop, using tips from the above post and elsewhere.
In my theme archive.php file, above the loop, I list the subcategories (optional):
<?php
$current_cat = get_queried_object();
$args = array( 'parent'=>$current_cat->term_id, 'child_of' => $current_cat->term_id, );
$categories = get_categories( $args );
foreach($categories as $category) { ?>
<h2><?php echo $category->name ;?></h2>
<p> etc....</p>
<?php } ?>
In my functions.php file, I've added the following custom function using pre_get_posts:
add_action( 'pre_get_posts', 'main_query_without_subcategory_posts' );
function main_query_without_subcategory_posts( $query ) {
if ( ! is_admin() && $query->is_main_query() ) {
// Not a query for an admin page.
// It's the main query for a front end page of your site.
if ( is_category() ) {
//Get the current category
$current_category = get_queried_object();
//get the id of the current category
$current_cat_id = $current_category->term_id;
//find the children of current category
$cat_args = array( 'parent'=>$current_category->term_id, 'child_of' => $current_category->term_id, );
$subcategories = get_categories( $cat_args );
//Get a list of subcategory ids, stick a minus sign in front
$subcat_id = array();
foreach($subcategories as $subcategory) {
$subcat_id[] = " -". $subcategory->term_id;
}
//join them together as a string with a comma seperator
$excludesubcatlist = join(',', $subcat_id);
//If you have multiple parameters, use $query->set multiple times
$query->set( 'posts_per_page', '10' );
$query->set( 'cat', ''.$current_cat_id.','.$excludesubcatlist.'' );
}
}
}
Then in the archive.php, below the subcategories, I've added the regular WordPress loop which is now being modified by the above function:
<?php while (have_posts() ) : the_post(); ?>
<h2><?php the_title();?></h2>
<p> etc....</p>
<?php endwhile;?>
Though the WordPress codex says that using "category__in" will exclude posts from subcategories, that didn't work for me and subcategory posts were still showing.
https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
https://developer.wordpress.org/reference/hooks/pre_get_posts/
I am wondering if there is a function in Wordpress where I can get all the posts in specific cateogires and order them according to category so each category would have an array of posts that belong to it.
I tried to us get_posts function but with no luck:
$args = array(
'numberposts' => 15,
'category' => '161,165,166,1',
);
$postslist = get_posts($args);
You should put ID of WP post and with functon get_post for retrieving post data as array.
<?php
$my_id = 7;
$post_id_7 = get_post($my_id, ARRAY_A);
$title = $post_id_7['post_title'];
?>
Full reference: http://codex.wordpress.org/Function_Reference/get_post
If you want get a posts by category id use:
$post_categories = wp_get_post_categories( $post_id );
$cats = array();
foreach($post_categories as $c){
$cat = get_category( $c );
$cats[] = array( 'name' => $cat->name, 'slug' => $cat->slug );
}
or dump with full data:
var_dump($cat);
You should use query_posts and the category__in or category__and argument - http://codex.wordpress.org/Function_Reference/query_posts
$args = array( 'category__in' => array(161,165,166,1), 'posts_per_page' => 15 );
query_posts( $args );
while (have_posts()): the_post();
the_title();
endwhile;