I am facing a very strange problem, all my posts are displaying in proper order but one post is not following the order. Here is the picture of dashboard side:
You can see in the picture Chapter 3 is displaying in the correct order.
Now that's the picture on the front end:
In the dropdown you can see Chapter 3 Gases is displaying in last while it should display after chapter 2. I'm not getting what's the problem Here is my PHP code for dropdown:
<ul class="dropdown-menu" aria-labelledby="dLabel">
<?php
$terms = get_the_terms( $post->ID, 'subject' );
if ( !empty( $terms ) ){
// get the first term
$term = array_shift( $terms );
// echo $term->name;
}
$classes = get_the_terms( $post->ID, 'class' );
if ( !empty( $classes ) ){
// get the first term
$class = array_shift( $classes );
// echo $term->name;
}
$args = array(
'post_type' => 'lecture',
'posts_per_page'=> -1,
'orderby' => 'date',
'order' => 'ASC',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'class',
'field' => 'slug',
'terms' => $class,
),
array(
'taxonomy' => 'subject',
'field' => 'slug',
'terms' => $term,
),
)
);
$data = new WP_QUERY($args);
$unique_chapter = array();
while($data->have_posts()):
$data->the_post();
$chapter_obj = get_field('chapter');
if( ! in_array( $chapter_obj, $unique_chapter ) ) :
// add city to array so it doesn't repeat
$unique_chapter[] = $chapter_obj;
?>
<li><a href="<?php echo the_permalink(); ?>">
<div class="text-left chapter_heading_topnav"><?php echo substr($chapter_obj->post_title,0,9) ;?></div>
<div class="text-left chapter_text_topnav"><?php echo $chapter_obj->post_content ;?></div>
</a></li>
<li class="divider"></li>
<?php endif; ?>
<?php endwhile; wp_reset_query(); ?>
</ul>
As you can see I'm setting order on date bases in ascending all other chapters are displaying properly only chapter 3 is out order. Please any clue why it's happening I'll be very thankful to you for your kind help.
I checked your site, it seems to be okay now. :)
Related
(first of all i'm new with php)
i have a wordpress blog where i have written about some ppl and i added their names as tags in a costum taxonomy to avoid multi language field with polylang.
I was looking for a loop that allow to show, for every tag, the title of post where tag exist.
Example => post's name: "what a chef can do?" (tagged Alex P.)
result: -Alex P. "what a chef can do?" 05/10/2020 - "Tomatos in the world" 15/12/2020.
For every person(tag) i have to make an Accordion.
i've started to write code but i'm not looking for a solution and i'm getting lost.
<?php $tag_args = array(
'orderby' => 'title',
'order' => 'ASC'
);
?>
<ul id="Genre-List">
<li>DEFAULT</li>
<?php
$terms = get_terms(
array(
'taxonomy' => 'chef',
'hide_empty' => false,
)
);
// Check if any term exists
if ( ! empty( $terms ) && is_array( $terms ) ) {
// Run a loop and print them all
print_r($terms);
foreach ( $terms as $term ) {
?>
<li><a href="<?php echo esc_url( get_term_link( $term ) ) ?>">
<?php echo $term->name; ?>
</a></li>
<?php
}
}
?>
Thanks if anyone can help about this.. im really getting mad.
If you are looking to get all posts which have any custom taxonomy term associated with it, you will need to query posts using a tax_query rather than simply retrieve all of the terms.
// get 'chef' taxonomy
$terms = get_terms([
'taxonomy' => 'chef',
'hide_empty' => false,
]);
// get any posts with one of the 'chef' taxonomy terms associated with it
$query = new WP_Query([
'post_type' => 'post',
'tax_query' => [[
'taxonomy' => 'chef',
'field' => 'term_id',
'terms' => array_column($terms, 'term_id'),
'operator' => 'IN',
]],
]);
// loop and output posts
while ($query->have_posts()) {
$query->the_post();
var_dump(get_the_title());
}
How to display all categories of a custom post type on home screen without listing the items.
I already created the custom post type and it's categories, now I need to display all the categories on my home page as links to the each category page. Can someone help please?
You can use now get_categories
Here is an example of code:
<?php
$args = array(
'taxonomy' => 'Your Taxonomy Name',
'hide_empty' => 0,
'orderby' => 'name'
);
$cats = get_categories($args);
foreach($cats as $cat) {
?>
<a href="<?php echo get_category_link($cat->slug); ?>">
<?php echo $cat->name; ?>
</a>
<?php
}
?>
Remember write your taxonomy name as you registered, in here 'Your Taxonomy Name'
e.g. product_cat, blog_cat etc
Hope this will help you.
$cat_args = array(
'taxonomy' => 'your-custom-post', // your custom post type
);
$custom_terms = get_categories($cat_args);
echo print_r($custom_terms);
<?php
$terms = get_terms( 'taxonamy_name', array(
'orderby' => 'count',
'hide_empty' => 0
) );
foreach($terms as $term)
{
echo $term->name;
}?>
</ul>
</div>
$args = array( 'post_type' => 'post', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
I'm developing a Latin Literature digital library for my thesis.
Right now I'm working on the author's archive page.
Authors is a Custom Post Type registered as 'auctores' with a custom taxonomy assigned to it named 'periodi'.
In the archive page (archive-auctores.php) I'd like to show all the authors listed by a meta field and other three columns: Periods (which refers to the custom taxonomy that I'd like to make a filter), Number of books (there should be the number of books (cpt) assigned to that author) and Gener (where to display all the literary genres of the books assigned to that author and it would be another taxonomy assigned to the book cpt via the cpt-onomies plugin). The number column should be sortable (asc/desc) while i'd like to make a filter for periods and for kinds.
I.E. --> opening the archive page it will show up the complete list of authors. So it should be possible to filter the authors for period and the page should show just the authors "tagged" with that specific taxonomy term.
I thought I had found a solution here, but when I try to select one term in the drop down the list remain the same.
I'm surely missing something.
That's the code of my template right now:
<form method="post" action="<?php the_permalink()?>">
<select name="periodi" id="selectperiodo" class="postform" onchange="submit();">
<option value="">Tutti i periodi</option>
<?php
$args = array(
'orderby' => 'ID',
'order' => 'ASC',
'hide_empty' => false,
'fields' => 'all',
'hierarchical' => true,
'pad_counts' => false,
'get' => '',
'child_of' => 0,
'parent' => '',
'childless' => false,
'cache_domain' => 'core',
'update_term_meta_cache' => true,
'meta_query' => '',
'parent' => 0
);
$terms = get_terms('periodi', $args);
if ( $terms ) {
foreach ( $terms as $term ) {?>
<option <?php if($term->slug == $_POST['periodi']){ echo 'selected="selected"';} ?> value="<?php echo esc_attr( $term->slug )?>"><?php echo esc_html( $term->name ) ?></option>
<?php }
}
?>
</select>
</form>
<table class="dataTable table table-hover">
<tr>
<th>Nome</th>
<th>Periodo</th>
<th>Opere</th>
<th>Genere</th>
</tr>
<?php $auctores_query = new WP_Query(array(
'post_type' => 'auctores',
'posts_per_page' => -1,
'order' => 'ASC',
'meta_key' => 'nome_classico',
'orderby' => 'meta_value',
)
); ?>
<?php $j = 0 ?>
<?php while ($auctores_query->have_posts()) : $auctores_query->the_post(); ?>
<?php $additional_class = (++$j % 2 == 0) ? 'even' : 'odd'; ?>
<tr class="<?php echo $additional_class ?>">
<td><?php the_title( '<h3 class="entry-title">', '</h3>' )?></td>
<td>
<?php
$terms = get_the_terms( $post->ID, 'periodi' );
if ( $terms && ! is_wp_error( $terms ) ) :
$periodi_links = array();
foreach ( $terms as $term ) {
$periodi_links[] = $term->name;
}
$on_draught = join( ", ", $periodi_links );
?>
<?php echo $on_draught; ?>
<?php endif; ?>
</td>
<td><span class="badge"><?php
$args = array('post_type' => 'texti',
'tax_query' => array (
array ( 'taxonomy' => 'auctores',
'field' => 'id',
'terms' => get_the_ID()
)
));
$query = new WP_Query( $args );
// the query
echo $query->found_posts;
?></span></td>
</tr>
<?php endwhile; ?>
</table>
Any help would be really appreciated.
Thanks!
IF you want the list of authors to be limited based on the drop-down selection, then you'll need to modify the query and utilize the Taxonomy Query Parameters:
Modify the code so that it responds to the form post as follows:
// First we have to take the args out into a variable
$args = array(
'post_type' => 'auctores',
'posts_per_page' => -1,
'order' => 'ASC',
'meta_key' => 'nome_classico',
'orderby' => 'meta_value'
);
// Then, we watch for the post and modify the args if appropriate:
if ( ! empty( $_POST['periodi'] ) ) {
$args['tax_query'] = array(
array(
'taxonomy' => 'periodi',
'field' => 'slug',
'terms' => $_POST['periodi']
)
);
}
// Then we can execute the query:
$auctores_query = new WP_Query( $args );
I've been running in to a problem.
I'm editing a custom wordpress theme. I want to make some adjusments to the FAQ page (created with custom post type).
Right now every subject of question (= also category) is showing only 5 answers (these are posts).
I wanted to know, how I could increate this number, so instead of 5, show 10 or rather, show all answers per subject.
This is the code:
archive-faq.php
<?php $terms = get_terms( 'faq_category' ); ?>
<?php foreach( $terms as $term ):
$args = array (
'post_type' => 'faq',
'tax_query' => array(
array(
'taxonomy' => 'faq_category',
'field' => 'id',
'terms' => $term->term_id,
),
),
);
$query = new WP_Query( $args ); ?>
<h1><?php echo $term->name; ?></h1>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="toggle-holder">
<h5 class="toggle"><span><?php the_title(); ?></span></h5>
<div class="toggle-box">
<div>
<?php the_content(); ?>
</div>
</div>
</div>
<?php endwhile; ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
An important part is:
<div class="toggle-box">
<div>
<?php the_content(); ?>
</div>
</div>
Where the_content() , is showing the posts that are in a certain category.
However it's nowhere to be found why , the page is only showing up to 5 posts (answers) and not any more ?
Things I've tried:
$query = new WP_Query( $args );
$query_posts('post_per_page=3'); ?>
Also:
putting 'showposts' => 8 under 'terms' => $term->term_id,
And I also tried:
$query = new WP_Query( $args ); ?>
<?PHP
query_posts( array(
'workcap' => $all_post_terms,
'showposts' => 8,
'caller_get_posts' => 1,
'post__not_in' => $do_not_duplicate ) ); ?>
->> In summary:
Why does the page only show up to 5 posts ?
And how do I change this property ?
PS. If you want to see the page: http://goo.gl/UnWRTz
The argument is posts_per_page and not post_per_page. Its a typo?
Try this:
$args = array (
'post_type' => 'faq',
'posts_per_page' => -1,
//for now try without this taxonomy, if it works try with it.
/*'tax_query' => array(
array(
'taxonomy' => 'faq_category',
'field' => 'id',
'terms' => $term->term_id,
),
),*/
);
Btw, you can find more info here: https://codex.wordpress.org/Class_Reference/WP_Query
I currently have the option in my CMS, to add tags to my custom post type single page.
Now, I am wanting to display this tag as a 'featured' item.
So, In my taxonomy-'filename', I use the following code which gathers the tags and displays them in the taxonomy page:
<?php
$args = array(
'tag_slug__and' => array('sector1'),
'post_type' => array( 'sectors' )
);
$loop = new WP_Query( $args );
while ($loop->have_posts() ) : $loop->the_post();
?>
<a href="<?php echo get_permalink(); ?>">
<?php echo "<div class='col-md-6' style='margin-bottom:20px;'>"; ?>
<div class="row mobilemargin">
<div class="categorytiletextsector1">
<div class="col-md-6 col-sm-6 col-xs-12 nopr"><?php echo get_the_post_thumbnail( $page->ID, 'categoryimage', array('class' => 'sector1img hovereffect')); ?> </div>
<div class="col-md-6 col-sm-6 col-xs-12">
<div class="testdiv">
<h5><?php the_title(); ?></h5>
<p><?php the_excerpt(); ?></p>
</div>
</div>
</div>
</div>
<?php echo "</div>"; ?>
</a>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
Now, my issue is, this will display the selected tag on every category page now, as it is set on the taxonomy page.
How can I make this only set on the current category.
So If my item is in the 'category A', only the category page of 'A' would show this, using the items category?
Any help would be great
Edit.
Used this code, hoping this should work, but no luck
$args = array(
'tag_slug__and' => array( 'sector1' ),
'post_type' => array( 'sectors' ),
'tax_query' => array(
array(
'taxonomy' => 'sectors',
'terms' => get_queried_object_id(),
),
),
);
Your problem is your custom query. One very important note here is, never ever change replace the main query with a custom one on any type of archive page or the home page. I have explained everything in detail in this post recently. Make sure to read it and all the linked posts as this will benefit you a lot
Your solution would be to remove your custom query and replace this with the default loop that we all know
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
// Your template tags and html mark up
}
}
If you need to change anything in the main query, use pre_get_posts to do so
EDIT
Your best idea here would be to use a full tax_query to display posts that is in the selected taxonomy term and tag
You can try something like this: (Requires at least PHP 5.4+. Also, this untested)
$q = get_queried_object();
$args = [
'post_type' => 'sectors',
'tax_query' => [
[
'taxonomy' => $q->taxonomy,
'terms' => $q->term_id,
'include_children' => false // Exclude child terms
],
[
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'sector1', //I believe this is the slug
],
],
];
For older PHP versions, use the following
$q = get_queried_object();
$args = array(
'post_type' => 'sectors',
'tax_query' => array(
array(
'taxonomy' => $q->taxonomy,
'terms' => $q->term_id,
'include_children' => false // Exclude child terms
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'sector1', //I believe this is the slug
),
),
);
EDIT 2
To exclude posts that are in the sector1 tag and any other sectorX tag, you can do the following
You can try something like this: (Requires at least PHP 5.4+. Also, this untested)
$q = get_queried_object();
$args = [
'post_type' => 'sectors',
'tax_query' => [
[
'taxonomy' => $q->taxonomy,
'terms' => $q->term_id,
'include_children' => false // Exclude child terms
],
[
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'sector1', //I believe this is the slug
'operator' => 'NOT_IN'
],
],
];
For older PHP versions, use the following
$q = get_queried_object();
$args = array(
'post_type' => 'sectors',
'tax_query' => array(
array(
'taxonomy' => $q->taxonomy,
'terms' => $q->term_id,
'include_children' => false // Exclude child terms
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'sector1', //I believe this is the slug
'operator' => 'NOT_IN'
),
),
);
Just note, you can pass an array of tags to the terms parameter like this
'terms' => array( 'sector1', 'sector2', 'etc' ),
or short array syntax
'terms' => ['sector1', 'sector2', 'etc'],
EDIT 3
As this is your main query, you need to make a few changes. As I have said, remove the custom query. Your main loop should look something like this
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<a href="<?php echo get_permalink(); ?>">
<?php echo "<div class='col-md-6 col-sm-6 col-xs-12' style='margin-bottom:30px;'>"; ?>
<div class="row mobilemargin">
<div class="categorytiletext2">
<div class="col-md-6 col-sm-12 col-xs-12 nopr"><?php echo get_the_post_thumbnail( $page->ID, 'categoryimage', array('class' => 'hovereffect newimgheight')); ?> </div>
<div class="col-md-6 col-sm-12 col-xs-12 mobilewhite">
<div class="testdiv">
<h5 class="captext"><?php the_title(); ?></h5>
<?php $trimexcerpt = get_the_excerpt();
$shortexcerpt = wp_trim_words( $trimexcerpt, $num_words = 10, $more = '… ' );
echo '<p>' . $shortexcerpt . '</p>';
?>
</div>
</div>
</div>
</div>
<?php echo "</div>"; ?>
</a>
<!-- If there is no posts, display an error message -->
<?php endwhile;
else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
<!-- If there is no posts, display an error message -->
You can now use pre_get_posts to remove the desired tag from your taxonomy pages. In your functions.php, do the following: (Requires PHP 5.3+, and is also untested)
add_action( 'pre_get_posts', function ( $q )
{
if ( !is_admin() && $q->is_main_query() && $q->is_tax() ) {
$q->set( 'tag__not_in', array( 145 ) );
}
});
For older versions use
add_action( 'pre_get_posts', 'so30256167_remove_tags' );
function so30256167_remove_tags( $q )
{
if ( !is_admin() && $q->is_main_query() && $q->is_tax() ) {
$q->set( 'tag__not_in', array( 145 ) );
}
}
Just remember to change 145 to your exact tag id or an array of tagids
EDIT 4
If you don't have the tag ids, you can use get_term_by() to get the tag id from the tag slug. Something like this will do: (Requires PHP 5.3+, and is also untested)
add_action( 'pre_get_posts', function ( $q )
{
if ( !is_admin() && $q->is_main_query() && $q->is_tax() ) {
$tag_object = get_term_by( 'slug', 'sector1', 'post_tag' );
$tagID = $tag_object->term_id;
$q->set( 'tag__not_in', array( $tagID ) );
}
});
For older versions use
add_action( 'pre_get_posts', 'so30256167_remove_tags' );
function so30256167_remove_tags( $q )
{
if ( !is_admin() && $q->is_main_query() && $q->is_tax() ) {
$tag_object = get_term_by( 'slug', 'sector1', 'post_tag' );
$tagID = $tag_object->term_id;
$q->set( 'tag__not_in', array( $tagID ) );
}
}
If you have an array of tag slugs, you can replace the following
$tag_object = get_term_by( 'slug', 'sector1', 'post_tag' );
$tagID = $tag_object->term_id;
$q->set( 'tag__not_in', array( $tagID ) );/*
with
$tag_array = array( 'slug1', 'slug2', 'slug3' );
foreach ( $tag_array as $tag ) {
$tag_object = get_term_by( 'slug', $tag, 'post_tag' );
$tagids[] = $tag_object->term_id;
}
$q->set( 'tag__not_in', $tagids );
Just remember to change the slugs accordingly
EDIT 5
Your final code in functions.php with pre_get_posts should be
add_action( 'pre_get_posts', 'so30256167_remove_tags' );
function so30256167_remove_tags( $q )
{
if ( !is_admin() && $q->is_main_query() && $q->is_tax() ) {
$tag_array = array( 'sector1', 'sector2', 'sector3', 'sector4' );
foreach ( $tag_array as $tag ) {
$tag_object = get_term_by( 'slug', $tag, 'post_tag' );
$tagids[] = $tag_object->term_id;
}
$q->set( 'tag__not_in', $tagids );
}
}