WORDPRESS Simple Query - php

I have a category query, and in my category query I want to get product (only one) by queried category id (or name or whatsoever)
I start query:
<?wpsc_start_category_query(array('category_group'=> get_option('wpsc_default_category'))); ?>
and then try to use get_posts() function to get product:
$args = array(
'post_type' => 'wpsc-product',
'posts_per_page' => 1,
'tax_query' => array(
array(
'taxonomy' => 'wpsc_product_category',
'field' => 'id',
'terms' => $aka
)));
$cat1_posts = get_posts($args);
where $aka is:
$aka = '[wpsc_category_id]';
but when I echo $cat1_posts[0]->ID; it only shows my last product ID for every category. what is the problem? echoing only [wpsc_category_id] works perfect.
I tried EVERYTHING for the last few days. I will buy you cookies for help
I've got to idea, that I need foreach or anything like this

You can use the get_terms() function. So something like this (untested)
<?php
//for each category, show latest post
$cat_args=array(
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_terms( 'wpsc_product_category');
foreach($categories as $category) {
$args=array(
'showposts' => 1,
'post_type' => 'wpsc-product',
'wpsc_product_category' => array($category->slug)
);
$posts=get_posts($args);
if ($posts) {
echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
foreach($posts as $post) {
setup_postdata($post); ?>
<p><?php the_title(); ?></p>
<?php
} // foreach($posts
} // if ($posts
} // foreach($categories
?>

Related

Custom Post - add new class in categories once it's active

I tried different solution from internet but it doesn't work in my code. My goal is to add "active class" in categories when it's active. here is my working code for get_categories
<?php
$args = array(
'orderby' => 'slug',
'order' => 'ASC',
'parent' => 0,
'hide_empty' => false
);
$categories = get_categories( $args );
foreach( $categories as $category ){
echo '<li><a class="ctg" href="'. get_category_link( $category->term_id ) .' "><p>' . $category->name . '</p></a></li>';
}
?>

Printing links individually in Worppress

I basically want to create a category in the "Links" section of wordpress and add a number of links and titles for that category. Simple stuff.
I then want to be able to, in my template file, echo the links and title or anything about the link individually as i please. Preferably in a loop as I have some page building to do before and after the links.
I know 'category_before' and 'category_after' exist but they won't do what I need.
So I tried,
<?php $args = array(
'orderby' => 'name',
'order' => 'ASC',
'limit' => -1,
'category' => '3',
'hide_invisible' => 1,
'show_updated' => 0,
'echo' => 1,
'categorize' => 0,
'category_orderby' => 'name',
'category_order' => 'ASC',
'class' => 'linkcat',
'category_before' => '<tr><td>',
'category_after' => '</td></tr>' );
wp_list_bookmarks( $args );
?>
But that does a few things wrong. I don't need the category title or anything else besides the link text and destination really.
I am hoping to have a 'for' loop that will loop all links and I can just build my code section and links inside that, but let me know if there is a better way.
Thanks
EDIT: More Info
So I tried:
<?php
$taxonomy = 'link_category'; // Taken from the DB table
$tax_terms = get_terms( $taxonomy, array( 'hide_empty' => false ) );
?>
<ul>
<?php
foreach ($tax_terms as $tax_term) {
echo $tax_term->name;
}
?></ul>
Which is the closest I have got. This only returns the info about the category not what is in the category.
There is nothing in the "wp_term_taxonomy" table in the DB about the actually category I have made.
Thanks again
EDIT:
Here is the area I'm referring to:
I want to show these 2 links
You may want to try the get_bookmarks function.
$bookmarks = get_bookmarks( array(
'orderby' => 'name',
'order' => 'ASC',
'category_name' => 'category-name'
));
// Loop through each bookmark and print formatted output
foreach ( $bookmarks as $bookmark ) {
printf( '<a class="relatedlink" href="%s">%s</a><br />', $bookmark->link_url, $bookmark->link_name );
}
https://codex.wordpress.org/Function_Reference/get_bookmarks
For more control you can use the function get_terms :
<?php
//list terms in a given taxonomy
$taxonomy = 'category'; // Pass default category or any custom taxonomy name
$tax_terms = get_terms($taxonomy);
?>
<ul>
<?php
foreach ($tax_terms as $tax_term) {
echo '<li>' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a></li>';
}
?>
</ul>
For info functions & its parameter:
https://developer.wordpress.org/reference/functions/get_terms/

display wordpress posts by sub category

I am new in wordpress, I am using wordpress Version 4.5, I like to display posts by subcategories, anyone please help how can I do this.
This is what I want
PARENT CATEGORY NAME
SUB CATEGORY 1
Post 1
Post 2
SUB CATEGORY 2
Post 3
Post 4
SUB CATEGORY 3
Post 5
Post 6
...
Thanks in advance
If I am not wrong you need this, you need double loop to fetch the posts under subcategories
this is how you get current page category
<?php
$categories = get_the_category();
$catID = $categories[0]->cat_ID;
?>
and then do this by using above catID
<?php
$subcats = get_categories('child_of=' . $catID);
foreach($subcats as $subcat) {
echo '<h3>' . $subcat->cat_name . '</h3>';
echo '<ul>';
$subcat_posts = get_posts('cat=' . $subcat->cat_ID);
foreach($subcat_posts as $subcat_post) {
$postID = $subcat_post->ID;
echo '<li>';
echo '<a href="' . get_permalink($postID) . '">';
echo get_the_title($postID);
echo '</a>';
echo '</li>';
}
echo '</ul>';
}
?>
If you want to show all the categories that have subcategories with their respective subcategories that have posts with their respective posts, you can easily do that with the following function, as long as you know the slug of the taxonomy and the post type.
For example if you have a custom post type called 'books' and a custom taxonomy to classify your books called 'topic',
then you would call this function using
ow_categories_with_subcategories_and_posts( 'topic', 'book' );
That will display a list of all the topics with their respective subcategories followed by the books that belong to each subcategory.
Something like
Drama
drama subcategory 1
book1
book2
book3
drama subcategory 2
book4
book5
Comedy
comedy subcategory 1
book6
book7
comedy subcategory 2
book8
book9
The following is the code for this:
function ow_categories_with_subcategories_and_posts( $taxonomy, $post_type ) {
// Get the top categories that belong to the provided taxonomy (the ones without parent)
$categories = get_terms(
array(
'taxonomy' => $taxonomy,
'parent' => 0, // <-- No Parent
'orderby' => 'term_id',
'hide_empty' => true // <!-- change to false to also display empty ones
)
);
?>
<div>
<?php
// Iterate through all categories to display each individual category
foreach ( $categories as $category ) {
$cat_name = $category->name;
$cat_id = $category->term_id;
$cat_slug = $category->slug;
// Display the name of each individual category
echo '<h3>Category: ' . $cat_name . ' - ID: ' . $cat_id . ' - Slug: ' . $cat_slug . '</h3>';
// Get all the subcategories that belong to the current category
$subcategories = get_terms(
array(
'taxonomy' => $taxonomy,
'parent' => $cat_id, // <-- The parent is the current category
'orderby' => 'term_id',
'hide_empty' => true
)
);
?>
<div>
<?php
// Iterate through all subcategories to display each individual subcategory
foreach ( $subcategories as $subcategory ) {
$subcat_name = $subcategory->name;
$subcat_id = $subcategory->term_id;
$subcat_slug = $subcategory->slug;
// Display the name of each individual subcategory with ID and Slug
echo '<h4>Subcategory: ' . $subcat_name . ' - ID: ' . $subcat_id . ' - Slug: ' . $subcat_slug . '</h4>';
// Get all posts that belong to this specific subcategory
$posts = new WP_Query(
array(
'post_type' => $post_type,
'posts_per_page' => -1, // <-- Show all posts
'hide_empty' => true,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'terms' => $subcat_id,
'field' => 'id'
)
)
)
);
// If there are posts available within this subcategory
if ( $posts->have_posts() ):
?>
<div>
<?php
// As long as there are posts to show
while ( $posts->have_posts() ): $posts->the_post();
//Show the title of each post with the Post ID
?>
<p>Post: <?php the_title(); ?> - ID: <?php the_ID(); ?></p>
<?php
endwhile;
?>
</div>
<?php
else:
echo 'No posts found';
endif;
wp_reset_query();
}
?>
</div>
<?php
}
?>
</div>
<?php
}
Then if you ONLY want to get the subcategories related to comedy, and you want to provide the 'comedy' slug as a filter to get something like:
comedy subcategory 1
book6
book7
comedy subcategory 2
book8
book9
Then you would call the following function like this:
ow_subcategories_with_posts_by_category( 'topic', 'book', 'comedy' );
And the function to do that will be:
function ow_subcategories_with_posts_by_category( $taxonomy, $post_type, $term ) {
$category = get_term_by( 'slug', $term, $taxonomy );
$cat_id = $category->term_id;
// Get all subcategories related to the provided $category ($term)
$subcategories = get_terms(
array(
'taxonomy' => $taxonomy,
'parent' => $cat_id,
'orderby' => 'term_id',
'hide_empty' => true
)
);
?>
<div>
<?php
// Iterate through all subcategories to display each individual subcategory
foreach ( $subcategories as $subcategory ) {
$subcat_name = $subcategory->name;
$subcat_id = $subcategory->term_id;
$subcat_slug = $subcategory->slug;
// Display the name of each individual subcategory with ID and Slug
echo '<h4>Subcategory: ' . $subcat_name . ' - ID: ' . $subcat_id . ' - Slug: ' . $subcat_slug . '</h4>';
// Get all posts that belong to this specific subcategory
$posts = new WP_Query(
array(
'post_type' => $post_type,
'posts_per_page' => -1, // <-- Show all posts
'hide_empty' => true,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'terms' => $subcat_id,
'field' => 'id'
)
)
)
);
// If there are posts available within this subcategory
if ( $posts->have_posts() ):
?>
<div>
<?php
while ( $posts->have_posts() ): $posts->the_post();
//Show the title of each post with the Post ID
?>
<p>Post: <?php the_title(); ?> - ID: <?php the_ID(); ?></p>
<?php
endwhile;
?>
</div>
<?php
else:
echo 'No posts found';
endif;
wp_reset_query();
}
?>
</div>
<?php
}

How to display the most recent post of each category of a custom post type in wordpress

This question seems to have been asked in some varieties, But mine is quite specific.
I need to display the most recent post of all categories of a custom post type called "products". I need to display the category instead of the title and link to that cat's archive.
The problem is, What I am getting instead is only one post of that general post type. Any thoughts?
Here is my starting point:
<?php
$query = new WP_Query( array(
'posts_per_page' => 12,
'post_type' => 'products',
'post_status'=>'publish',
'orderby'=>'post_date',
'order' => 'DESC',
'cat' => $category->term_id,
'count'=>1,
));
if ( have_posts() ) : while ( $query->have_posts()) : $query->the_post(); ?>
<div class="col-sm-6 col-md-4">
<?php if ( has_post_thumbnail()) : ?>
<?php the_post_thumbnail('product', array('class' => 'img-responsive center-block')); ?><h3><?php the_title(); ?></h3>
<?php else : ?>
<!--<img class="img-responsive center-block" src="<?php echo THEME_DIR; ?>/img/default-thumb.png" alt="<?php the_title(); ?>" />--><img class="img-responsive " data-src="holder.js/100%174/auto" alt="Generic placeholder image"><h3><?php the_title(); ?></h3>
<?php endif; ?>
</div><!-- col-sm-6 -->
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
</div><!-- end row -->
<ul class="pager">
<li class="previous"><?php previous_posts_link(); ?></li>
<li class="next"><?php next_posts_link(); ?></li>
</ul>
<?php else : ?>
<h1>Not Found</h1>
<p>Click Here to return to the Home Page</p>
<?php endif; ?>
The custom post type was created via a 3rd plug into Super CPT Plugin
Here is the code:
add_action( 'after_setup_theme', 'setup_data_structures' );
function setup_data_structures() {
if ( ! class_exists( 'Super_Custom_Post_Type' ) )
return;
$products = new Super_Custom_Post_Type( 'products', 'Product', 'Products' );
# Test Icon. Should be a square grid.
$products->set_icon( 'suitcase' );
# Taxonomy test, should be like categories
$product_category = new Super_Custom_Taxonomy( 'product-category', 'Product Category', 'Product Categories', 'category' );
# Connect both of the above taxonomies with the post type
connect_types_and_taxes( $products, array($product_category) );
# Add a meta box with every field type
$products->add_meta_box( array(
'id' => 'product-fields',
'context' => 'normal',
'fields' => array(
'client-name' => array('column' => true ),
'product-description' => array( 'type' => 'wysiwyg' )
)
) );
$products->add_to_columns( 'product-category' );
This should help you out. The follow code displays 5 of each category. Just switch the 5 to how many ever you wish to display from each category.
<?php
$cat_args = array(
'orderby' => 'name',
'post_type' => 'products',
'order' => 'ASC',
'child_of' => 0
);
$categories = get_categories($cat_args);
foreach($categories as $category) {
echo '<dl>';
echo '<dt> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></dt>';
$post_args = array(
'numberposts' => 5,
'category' => $category->term_id
);
$posts = get_posts($post_args);
foreach($posts as $post) {
?>
<dd><?php the_title(); ?></dd>
<?php
}
echo '<dd class="view-all"> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>View all posts in ' . $category->name.'</a></dd>';
echo '</dl>';
}
?>
<?php
$cats = array('3d-mail', 'boxes', 'folders', 'binders');
$exclude_posts = array();
foreach( $cats as $cat )
{
// build query argument
$query_args = array(
'category_name' => $cat,
'showposts' => 1,
'post_type' => 'products',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC'
);
// exclude post that already have been fetched
// this would be useful if multiple category is assigned for same post
if( !empty($exclude_posts) )
$query_args['post__not_in'] = $exclude_posts;
// do query
$query = new WP_Query( $query_args );
// check if query have any post
if ( $query->have_posts() ) {
// start loop
while ( $query->have_posts() ) {
// set post global
$query->the_post();
// add current post id to exclusion array
$exclude_posts[] = get_the_ID();
<?php the_content(); ?>
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
}
?>

WordPress: Get all tags from custom post type

I'm trying to get all the tags that are within my custom post type "resource".
The problem is that I'm outside of the loop and struggling to find a way to get the functionality to work with the custom post type.
I have the category setup also as "resource_category"
My current code is:
$tax = 'post_tag';
$terms = get_terms( $tax );
$count = count( $terms );
if ( $count > 0 ): ?>
<div class="post-tags">
<?php
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, $tax );
echo '' . $term->name . ' ';
} ?>
</div>
<?php endif;
Can anyone help?
you are asking for the tags right, because the answer from 2015 is listing categories not tags
$args = array(
'type' => get_post_type(),
'orderby' => 'name',
'order' => 'ASC'
);
$tags = get_tags($args);
foreach($tags as $tag) {
var_dump($tag->name);
}
Tags are also WordPress taxonomy. So, you can get all the tags as like you get all terms
Read More
$tags = get_terms([
'taxonomy' => 'YOUR_CUSTOM_TAXONOMY',
'hide_empty' => false
]);
var_dump($tags);
You can also copy custom post taxonomy from Tags page URL.
http://localhost/wp-admin/edit-tags.php?taxonomy=YOUR_CUSTOM_POST_TAG_TAXONOMY_NAME&post_type=custom-post-type
$args = array(
'type' => 'resource',
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_categories($args);
foreach($categories as $category) {
echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
}

Categories