Only show WordPress 'standard' post format on template? - wordpress

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).

Related

How to display post with two custom taxonomy in wordpress

I have a custom post type - Poems.
And I create a two taxonomy - Cities and Authors.
When I clicked on "Paris" menu item - I need to displayed all Authors and his list of poems in that city.
How to do it ?
Thanks.
You'll likely want to take a look at the Codex entry for WP Query. That gives you all the information you could want on creating custom WordPress queries. Including taxonomy arguments.
You'd want to pass to tax query arguments using the AND relation. Something along the lines of this example:
$args = array(
'post_type' => 'post',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'city',
'field' => 'slug',
'terms' => 'london',
),
array(
'taxonomy' => 'author',
'field' => 'slug',
'terms' => 'dickens',
),
),
);
$query = new WP_Query( $args );
Edit: 10/30/17 per comment below
If you were saving your author's as post meta you could order by that. But if your author is also a taxonomy it gets a little trickier since there isn't a good way to order by a taxonomy. See this related question.
Instead we could get all of our author taxonomy terms using get_terms.
$authors = get_terms( 'author', array(
'hide_empty' => false,
) );
That gives us all author terms that have at least one post assigned to them.
Now that we have the terms we could run through each author and run a query to get the poems for that author in that city. Like so:
foreach ( $authors as $author ) {
$args = array(
'post_type' => 'poem',
'tax_query' => array(
relation => 'AND',
array(
'taxonomy' => 'city',
'field' => 'slug',
'terms' => 'london',
),
array(
'taxonomy' => 'author',
'field' => 'term_id',
'terms' => $author->term_id,
),
),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
// Put your post markup here
endwhile;
endif; wp_reset_query();
}
If you are not good with coding you can use free Content Views plugin, and combine that yyou have to combine. I started like this.

WP_Query to get posts with featured image OR video format posts without featured image

I'm currently working on a query where I show only posts without empty featured image using this:
$args = array(
'meta_query' => array(
array( 'key' => '_thumbnail_id')
)
);
Everything is working great. But I have some video format posts without featured images that I would like to return with that query.
Is that even possible with WP_Query?
Any help would be great!
Got it to work with 2 WP_Query and a array_merge, not sure if it's the best way to go but does the work.
If that can help:
get posts with thumb
$args = array(
'meta_query' => array(
array( 'key' => '_thumbnail_id'), //Show only posts with featured images
)
);
$posts_with_thumb = new WP_Query($args);
get video posts format
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-video' ),
),
)
);
$posts_video_format = new WP_Query($args);
init new wp_query object and merge both results
$featured_query = new WP_Query();
$featured_query->posts = array_merge( $posts_with_thumb->posts, $posts_video_format->posts );

how build a wordpress query_posts based on url

I want to use the query_posts function from wordpress for displaying the right posts. I want to use the url www.site.com/?s=taxonomy1=test1&taxonomy2=test2 for building the query. something like down here:
$taxonomy1 = $_GET['taxonomy1'];
$taxonomy2 = $_GET['taxonomy2'];
query_posts(array(
'posts_per_page' => 10,
'taxonomy1' => $taxonomy1,
'taxonomy2' => $taxonomy2,
) );>
How i do this?
the wordpress codex is your best friend when trying to build custom queries. something like this should work as a query
$taxonomy1 = $_GET['taxonomy1'];
$taxonomy2 = $_GET['taxonomy2'];
$the_query = new WP_Query(array(
'posts_per_page' => 10,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => $taxonomy1,
'field' => 'slug',
),
array(
'taxonomy' => $taxonomy2,
'field' => 'slug',
),
),
));
and to display the results
while ( $the_query->have_posts() ) : $the_query->the_post();
//the_title(), the_content(), etc
endwhile;
note that the query is using the new (as of 3.1) method for querying taxonomies. As you can see it gives you a lot more flexibility and i would suggest reading through the above link

Get multiple posts by multiple slugs

I have an array with multiple slugs (post names) and I want to get all posts where the slug is in the array.
I use Event Organsier and want to get all events these slugs. I haven't worked with WordPress for a couple of years so I need some help.
$events = eo_get_events(array(
'numberposts'=>5,
'event_start_after'=> $year.'-'.$month.'-'.$list_day,
'event_end_before'=> $year.'-'.$month.'-'.$list_day,
'showpastevents'=>false,//Will be deprecated, but set it to true to play it safe.
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array('slug-example-1', 'slug-example-2'),
'operator' => 'IN'
)
)
));
This should (imo) return all posts that has a slug that is slug-example-1 or slug-example-2.
I don't even know if tax_query/taxonomy is the right thing to use.
You can use the post_name__in argument in conjunction with get_posts(), it's available since Version 4.4.
E.g. :
$args = [
'post_name__in' => [ 'slug-1', 'slug-2' ],
'post_type' => 'page',
'post_status' => 'publish',
];
$result = get_posts( $args );

Display all posts of a post format.

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.

Resources