Before, sorry for my bad grammar/English and bad code understanding
I'm testing a new website about short or series story with custom post type generated by Writeshare plugin. What I would like to achieve on my dynamic page is like this
Custom term (term name: Short Stories | slug: short term | tag ID: 2) from custom taxonomies (tax name: Format)
Post | Post | Post | Post | Post
Custom term (term name: Series | slug: series | tag ID: 3) from custom taxonomies (tax name: Format)
Post | Post | Post | Post | Post
My website is: https://qisa.xyz/ | and I'm using justread theme https://wordpress.org/themes/justread/
but I always failed when try to adding the tax to the loop, while custom post type (called WPWS content) successfully added to the home page via pre_get_post in functions.php
I've been trying modified the code trying to only get post from needed taxonomy in pre_get_post function and loop but nothing worked so far.
Now my code is consisting default first loop (all content from custom post type, worked perfectly) and second loop (modified to specific taxonomy, as I write this thread in 'nothing found' status
And what the best approach to achieve this. Adding new WP_query? or through pre_get_post function?
code in function.php
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
if ( is_home() && $query->is_main_query() )
$query->set( 'post_type', array( 'wpws_content' ) );
return $query;
}
now code in index.php
<?php
if ( have_posts() ) :
/* Start the Loop */
while ( have_posts() ) : the_post();
/*
* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'template-parts/content', get_post_format() );
endwhile;
else :
get_template_part( 'template-parts/content', 'none' );
endif;
?>
</main><!-- #main -->
<?php the_posts_pagination(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main grid grid--4">
<?php $temp_query = clone $wp_query; ?>
<?php query_posts( 'taxonomy_name=short' ); ?>
<?php
if ( have_posts() ) :
/* Start the Loop */
while ( $temp_query->have_posts() ) : $temp_query->the_post();
/*
* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'template-parts/content', get_post_format() );
endwhile;
else :
get_template_part( 'template-parts/content', 'none' );
endif;
?>
Update:
I tried implementing the solution from Manoj Webvertex, but that was sent me into technical error. Where did the code go wrong?
<?php
$post_type = 'post';
// Get all the taxonomies for this post type $taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) :
// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( $taxonomy );
foreach( $terms as $term ) :
$posts = new WP_Query( "taxonomy=$format&term=$short->slug&posts_per_page=5" );
if( $posts->have_posts() ):
while( $posts->have_posts() ) : $posts->the_post();
/*
* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'template-parts/content', get_post_format() );
endwhile;
else :
get_template_part( 'template-parts/content', 'none' );
endif;
<?php wp_reset_query(); ?>'
<?php
$post_type = 'post';
// Get all the taxonomies for this post type $taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) :
// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( $taxonomy );
foreach( $terms as $term ) :
$posts = new WP_Query( "taxonomy=$taxonomy&term=$term->slug&posts_per_page=2" );
if( $posts->have_posts() ):
while( $posts->have_posts() ) : $posts->the_post();
//Do you general query loop here
endwhile;
endif;
endforeach;
endforeach;
Related
I have a custom taxonomy called mwp_ss_supp. I have a page that displays all the posts using this code:
<?php $count = wp_count_posts('mwp_ss_supp')->publish;
echo '<div id="post-count">' . $count . ' posts displayed</div>';?>
<?php
if ( have_posts() ) :
while (have_posts()) : the_post();
DO STUFF
<?php
endwhile;
endif; ?>
The problem is that all the posts are displaying in the order of latest post is the first on the list published. But I want to print them in a different order (ie, posts 1-10 are published first, posts 15-20 are published next, and 11-14 are published last, which ends up being published in alphabetical order by title). So how do I access this array of posts? There doesn't seem to be an array variable to play around with.
Show sql query for that. The sql query gets data from the database and determines the sort
Try adding a pre_get_posts() modifier to your theme's functions.php:
function taxo_posts($query)
{
if ($query->is_tax('mwp_ss_supp') && $query->is_main_query())
{
$query->set( 'orderby', 'title' );
$query->set( 'order', 'ASC' ); // or DESC
}
}
add_action('pre_get_posts', 'taxo_posts');
You need to build a custom wordpress query WP_Query() to query posts against taxonomies:
<?php
$args = array(
'post_type' => 'custom_post_type',
'tax_query' => array(
array(
'taxonomy' => 'custom_taxonomy_type',
);
);
'orderby'=> 'title',
'order' => 'ASC',
);
/**
* WP_Query()
* The WordPress Query class.
* #link https://developer.wordpress.org/reference/classes/wp_query/
*/
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
if( get_the_title() !== '' ):
the_title( '<h1>', '</h1>' );
endif;
if( get_the_content() !== '' ):
the_content();
endif;
endwhile;
endif;
/**
* wp_reset_postdata()
* After looping through a separate query, this function restores the $post global to the current post in the main query.
* #link https://developer.wordpress.org/reference/functions/wp_reset_postdata/
*/
wp_reset_postdata(); ?>
Learn more
wp_query # https://developer.wordpress.org/reference/classes/wp_query/
I need display the custom taxonomy thumnbail and name on category page. I have this code :
<h1><?php single_cat_title(); ?></h1>
<?php
$taxonomy = 'kategorie_goralskie';
$terms = get_terms($taxonomy, array('parent' => 0, 'orderby' =>
'meta_value', 'order' => 'DESC') ); // Get all top level terms of a
taxonomy
if ( $terms && !is_wp_error( $terms ) ) :
?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="col-md-3">
<?php the_title();?>
</div>
<?php endwhile; ?>
<?php endif; ?>
It shows only name of child (its ok) I must show thumnail too. How i can do it ? I tried in many ways
enter image description here
In front-page.php everything is ok, i see taxonomy parent - just look : enter image description here
You need to call the_post_thumbnail function somewhere in your loop to display thumbnail. you can pass the size of the thumbnail you whant in function parameter.
See the detail of this function here : https://developer.wordpress.org/reference/functions/the_post_thumbnail/
I need to query my pages to display on blog posts. But not all pages are going to be displayed so I've use metabox and create conditional checkbox if its going to be displayed or not.
My problem is when I query the post and pages, it doesn't show the posts/pages that are allowed to be shown on the blog page.
Assume that the meta_value is 1 which is set not to display and meta_value 0 is set display.
Below is my code:
$args = array(
'post_type' => array('post', 'page' ),
'meta_key' => 'gz_checkbox',
'meta_value' => '1',
'meta_compare' => '!='
);
query_posts($args);
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
/* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'no-results', 'index' ); ?>
<?php endif; ?>
I'm using the basic loop code in a taxonomy archive (artists) and I was wondering how you can set the loop to show posts in random order ('orderby'=>'rand') it doesn't seem to work when I add the array? Any help would be great!
<?php
// Start the Loop.
while ( have_posts() ) : the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
array ( 'orderby' => 'RAND' );
get_template_part( 'content', get_post_format() );
endwhile;
// Previous/next page navigation.
twentyfourteen_paging_nav();
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;
?>
<?php
$query = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '-1' ) );
if( $query->have_posts() ):
// Start the Loop.
while ( $query->have_posts() ) : $query->the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
endwhile;
// Previous/next page navigation.
twentyfourteen_paging_nav();
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;
?>
more info for query
query_posts(array(
'showposts' => 6,
'orderby' => 'rand',
'category_name' => 'News' //You can insert any category name
));
Nice Question first !
You can do that with simple using function of PHP.
http://www.php.net/manual/en/function.shuffle.php
Follow below step:
First thing is get all post with query
You know that wordpress will provide the result in array format. So, do not try more coding its too complecated.
Now you have array of result so just use PHP function shuffle.
http://www.php.net/manual/en/function.shuffle.php
Please ask me after implementation if any query.
Thanks !
Try this:
<?php
$args = array(
'orderby' => 'rand'
);
$query = new WP_Query($args);
if (have_posts()) {
while ( $query->have_posts() ) : $query->the_post();
get_template_part( 'content', get_post_format() );
endwhile;
// Previous/next page navigation.
twentyfourteen_paging_nav();
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;
?>
You have two ways of doing it. The first way is not the best way, but it may be simpler for you to understand:
Using WP_Query
<?php
$args = array(
'orderby' => 'random'
);
$query = new WP_Query( $args );
if( $query->have_posts() ):
// Start the Loop.
while ( $query->have_posts() ) : $query->the_post();
get_template_part( 'content', get_post_format() );
endwhile;
// Previous/next page navigation.
twentyfourteen_paging_nav();
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;
Here, we'll be using a custom WP_Query object and orderby to get random posts.
Using pre_get_posts
The best way to do it is by using the pre_get_post action to modify the page output automatically. You might need some more coding though.
ok, here's what I'm trying to do:
I've got a custom post type called drinks-menu, a taxonomy called drinks-menu-categories, and a page template called template-drinks-menu.php.
The taxonomy has a bunch of terms that are heirarchical - Wine, with children White and Red; Beer, with children Pilsener, Stout, etc...
I want to use one loop to display all the posts from these terms in the same order that they're ordered by in the admin. Here's the code I've got so far:
<?php
$post_type = 'drinks-menu';
// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies( (object) array('post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) :
// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( $taxonomy );
foreach( $terms as $term ) :
echo '<h1>'.$term->name.'</h1>';
if ( $term->description !== '' ) {
echo '<div class="page_summary">'. $term->description .'</div>';
}
echo '<br>';
$posts = new WP_Query( "taxonomy=$taxonomy&term=$term->slug&posts_per_page=-1&orderby=id&order=DESC" );
if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post();
?>
<?php the_title(); ?>
<?php
if( get_field( "price" ) ): ?>
<?php echo '<span">'; the_field( "price" ); echo ' | '; the_field( "abv" ); echo '</span>';?>
<?php endif;
echo '<em>'; the_excerpt(); echo '</em><br><br>';
endwhile; endif;
endforeach;
endforeach;
?>
This is working well to bring all the posts from the terms onto the page, but not in order. You can see I tried taxonomy=$taxonomy&term=$term-slug&posts_per_page=-1&orderby=id&order=DESC but it's not working, everything shows in alphabetical order.
Any Wordpress gurus who can point me in the right direction? I hope I've been clear about the issue. Thanks :-)
Posts ordered in admin page by "menu_order";
If u want to order posts using query_posts (WP_Query constructor) u should use value of variable "orderby" in upper case -> "ID".