I want to display all posts of a post format (let's say: aside). In WordPress to display all posts of a certain category, just need to use this URL: mysite.com/category/mycategory. Is there a similar way to display all posts of a post format? Or any other way is also fine for me.
You could use tax_query parameters to get the posts by post_format. For example:
$args = array(
'post_type'=> 'post',
'post_status' => 'publish',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-aside' )
)
)
);
Then you can iterate over the results with get_posts() (or use WP_Query() and a standard a Loop):
$asides = get_posts( $args );
if ( count($asides) ) {
foreach ( $asides as $aside ) {
// do something here...
}
}
// this will get all 'quote' post format
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-quote' )
)
)
);
$query = new WP_query($args);
while($query->have_posts()) : $query->the_post();
// do whatever you want with it
endwhile;
WordPress creates archive pages for each post format automatically.
Use the get_post_format_link() method to generate the link to the WordPress archive for each post format (although this method does not work with the "standard" post format).
See The Loop.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
That code above list all of the post on your wordpress site, so to show all post from a category you need the use the if ( in_category('CategoryNumberHere ') function to only fetch post from a category. You must also know the number of the category in your WordPress installation.
Take a look at the linked page above, it has a full tutorial and layout of how to do such things. The code specific to categories is the second section down.
Related
Problem
I want to create shortcodes for my WordPress website which has a CPT => toernooi
The function for counting posts works with the following code:
// Get post count for cpt
add_shortcode('toernooiencount', 'post_count');
function post_count() {
$count_posts = wp_count_posts('toernooi');
$published_posts = $count_posts->publish;
echo $published_posts . ' ';
}
Now I want to create the same function for counting posts but in different taxonomies inside this same CPT. Tried to find a solution but could not find anything that worked.
The WordPress website wp_count_posts has no information on using different use cases.
Question
Can anyone point me in the right direction?
I think the easiest way would be going with WP_Query like this:
$args = array(
'post_type' => 'toernooi',
'post_status' => 'publish',
'posts_per_page' => - 1,
'tax_query' => array(
array(
'taxonomy' => 'your_taxonomy',
'field' => 'slug', //('id' o 'slug')
'terms' => 'my_term_slug_or_id',
),
),
);
$query = new WP_Query( $args );
echo "We have found {$query->post_count} posts witht the query";
I can't think about another solution, since wp_query internally always does "SELECT SQL_CALC_FOUND_ROWS" you will have that count w/o any further cost
I want to auto assign some tags to a post category
I am using the wp set post tags function in the functions.php file.
wp_set_post_tags( $post_id, 'tag1,tag2', true );
It works when I put in the post id number. I need help with looping through the posts from a category.
Hope someone can help!
Thanks
You need to write query and then apply the function for it. Let me share coding for this.
For custom post type and taxonomy the following code will work. Make sure to replace post_type , taxonomy and terms according to your value.
$args = array(
'post_type' => 'custom_post_type',
'tax_query' => array(
array(
'taxonomy' => 'custom_taxonomy_name',
'field' => 'term_id',
'terms' => array(1,2,3),
),
),
);
$the_query = new WP_Query($args);
For post type post there the query is simple
$args = array(
'post_type' => 'post',
'category__in' => array(11,12,13)
);
$the_query = new WP_Query( $args );
Make sure to replace category__in with category id you want to run the query.
After that please run the loop
<?php
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
wp_set_post_tags( $the_query->post->ID, 'tag1,tag2', true );
}
} else {
// No post found
}
/* Restore original Post Data */
wp_reset_postdata();
?>
I'm coding a wordpress website, but i've run into some problems.
I have through Advanced Custom Fields make a new post type with a category.
I want to display a specific category in my wordpress loop, but for some reason it will not work.
<?php
$args=array(
'post_type' => 'medlem',
'cat' => 4,
);
$medlem = new WP_Query($args);
<?php if ( $medlem->have_posts() ) : while ( $medlem->have_posts() ) : $medlem->the_post(); ?>
It just display all categories, which is not the meaning. Some help?
Are you sure that your Custom Post Type have registered to "category" taxonomy type?
What do you mean by 'I have through Advanced Custom Fields make a new post type with a category.', because ACF is for creating custom fields to existing post types.
I think you could try this code:
$args = array(
'post_type' => 'medlem',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => 4
)
)
);
As 'field' => 'term_id' is default you can skip this line.
For more information go to: http://codex.wordpress.org/Class_Reference/WP_Query
Very simple setup, I'm using 1 template for multiple taxonomies. So example:
I have basic category + customtypetaxonomy. Both of them have terms. I'm displaying the posts of these terms in the same template - category.php. Which works great and how I want it.
However, I want to make small changes in this template based on the taxonomie it is getting its posts from. But I cannot figure out what function I need to use to get this.
For example: single_cat_title() gives me the title of the term (category)
But what I need is either the slug or the ID. Is there a way to do this? Something like single_cat_id or single_cat_slug. I have tried $cat but it doesn't give me any output.
Any ideas?
Use WP_Query. Like this:
$ThisQuery = new WP_Query( array( 'category_name' => 'MyCategory',
'meta_key' => 'MyCustomFieldValue',
'posts_per_page' => 10 ) ); // Number of posts
if ( $ThisQuery->have_posts() ) : while ( $ThisQuery->have_posts() ) : $ThisQuery->the_post();
...
endwhile;
endif;
wp_reset_postdata();
Modify the array to include the elements that identify the posts you want to fetch
Another EXAMPLE:
<?php
$Args = array( //Category
'cat' => 5, // Category id.
'category_name' => 'MyCategory', // Category Name.
//Taxonomy
'tax_query' => array( 'relation' => 'AND', // Could be OR
array( 'taxonomy' => 'MyTaxonomy1',
'field' => 'MySlug1',
'terms' => array( 'MyTerm11',
'MyTerm12' ), ),
array( 'taxonomy' => 'MyTaxonomy2',
'field' => 'MySlug2',
'terms' => array( 'MyTerm21',
'MyTerm22',
'MyTerm23' ), ) ), );
$ThisQuery = new WP_Query( $Args );
if ( $ThisQuery->have_posts() ) : while ( $ThisQuery->have_posts() ) : $ThisQuery->the_post();
// Do Stuff
endwhile;
endif;
?>
I recently added post formats to my WordPress theme - on the blog page its fine as they are all styled accordingly. However on my home page template I only want to show 'standard' posts formats (no links, galleries, audio, video etc.).
In my theme options I can decide how many posts to display on the front page which is what 'dft_recent_number' is for.
Does anybody know how I can change the code below to exclude all but 'standard' post formats?
<?php
$query = new WP_Query();
$query->query('posts_per_page='.get_option('dft_recent_number'));
//Get the total amount of posts
$post_count = $query->post_count;
while ($query->have_posts()) : $query->the_post();
?>
Any help is much appreciated!
WP_Query does not seem to have a straightforward parameter for post_format.
From my quick research it appears that post formats are related through taxonomies. So, theoretically, using the taxonomy parameters should work.
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => 'post-format-standard',
)
)
);
$query = new WP_Query( $args );
Note: you'll need to update the taxonomy names and slugs for your blog. This should be the names you set in your functions.php file.
// there's no post-format-standard so you should write it like this to exclude all other postpformats
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array('post-format-quote','post-format-audio','post-format-gallery','post-format-image','post-format-link','post-format-video'),
'operator' => 'NOT IN'
)
I know that's old, but I was facing the same issue and even though I've found a solution I wondered what other have done to "fix" that.
I think a more scalable solution could be something like that:
$post_formats = get_theme_support( 'post-formats' );
$tax_query = false;
if ( $post_formats ) {
$tax_query = array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => $post_formats[0],
'operator' => 'NOT IN'
)
);
}
// WP_Query arguments
$args = array(
'post_type' => 'post',
'order' => 'DESC',
'orderby' => 'date',
'tax_query' => $tax_query
);
This would exclude the post formats that have been enabled and also will work in case WP will add more post formats (or add the ability to add more).