WP Sorting custom posts alphabetically by Title - php

Newbie here...
I have a custom post type of 'equipe' (team in portuguese). I am trying to sort these alphabetically by post title then display the_title so we have a alphabetical list of names.
I've done a search on here and tried a few fixes but Im struggling to get anything other that the standard order.
Any help would be much appreciated!
<?php
$args = array('orderby'=> 'title', 'order' => 'ASC', 'post_type' => 'equipe', 'posts_per_page' => -1, 'post_status' => 'publish' );
$q = new WP_Query($args);
while ( $q->have_posts() ) : $q->the_post();
?>
<h3><?php the_title(); ?></h3>
<?php
endwhile;
wp_reset_query();
?>

<?php
$args = array( 'post_type' => 'equipe', 'posts_per_page'=>5, 'orderby'=>'post_title','order'=>'ASC');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>

RESOLVED:
Ok the reason it was enforcing menu_order was because of a setting (F*ing checkbox) within the plugin Post Types Order.
I needed to un-check AUTO SORT
and check Use query ASC / DESC parameter
This then allowed me to adjust the array as follow (and discussed above):
$args = array('orderby' => 'title', 'order'=>'ASC', 'post_type' => 'equipe')
However I did need to add 'order'=>'ASC' into the other pages that sorted by the original query of menu_order.

Related

Limit excerpts of post types

I have two post types, the regular posts and a custom post type. Everything is working fine and I show only 5 posts. One as a full post and four as excerpts. The problem I have is that the excerpts is showing the latest posts, independent of post category. I want to show two of the posts and two of the custom post type.
$args = array(
'post_type' => array( 'post', 'tutorial' ),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
$count = 0;
while ( $query->have_posts() ) : $query->the_post();
if ( $count == 0 ) { ?>
<h2><?php the_title(); ?></h2>
<?php the_content();
$count ++;
} else { ?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt();
}
endwhile;
endif;
wp_reset_postdata();
?>
Expected output should be the latest post as a full post, as it is working now. Then it should display the two latest posts of post type post and two latest posts of post type tutorial.
Basicly you only need to sort by posttype
$args = array(
'post_type' => array( 'post', 'tutorial' ),
'orderby' => 'post_type',
'order' => 'ASC',
);
If you want to keep the sorting of date as a secondary sort this should work (not tested).
$args = array(
'post_type' => array( 'post', 'tutorial' ),
'orderby' => array ('post_type' => 'ASC', 'order' => 'DESC' ),
);
For more information check the WP_Query documentation
Keep in mind that if you have 5 posts newer then any of your tutorials, none will show.
To guarantee 3 posts and 2 tutorials you will need to split the code in 2 wp_query loops with the posts_per_page parameter.

Add meta data from function for orderby in wordpress

Newbee here
I need my post to be orderby by the number of votes from a plugin named wp ulike. but to get the number of votes, I need to run a function. I've tried every way I can think but didn't work.
currently here's my code:
<?php
function votecount(){
if (function_exists('wp_ulike_get_post_likes')):
echo wp_ulike_get_post_likes(get_the_ID());
endif;
}
add_post_meta($post_id, 'votecount', $votecount);
?>
<?php
$ctr = 1;
$args = array(
'post_type' => 'ico',
'posts_per_page' => -1,
'meta_key' => 'votecount',
'orderby' => 'meta_value',
'order' => 'DESC'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
Any help will be much appreciated.
You have to add filter posts_orderby. Please take reference from below url.
https://wordpress.stackexchange.com/questions/198610/how-to-filter-by-last-name-for-custom-post/198624#198624

Getting all posts matching custom post type, taxonomy and category

I currently have the following set up:
Custom post type called "workshops"
Within the workshop post type I have a taxonomy called "workshop-status"
Workshop status subsequently has categories for example "past-event"
I have been using the code below to fetch posts within a particular post type:
<?php $loop = new WP_Query( array( 'post_type' => 'workshops', 'posts_per_page' => -1 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
content here...
<?php endwhile; wp_reset_query(); ?>
My question is: how can I change this to fetch a post from the past-event category within my custom post type, and custom taxonomy?
My aim is to have multiple page templates and target each category individually.
I have tried changing the arrange to target the category alone, but this did not work. I cannot find an online resource on how to target all aspects.
You simply need to add the category attribute like so:
$query = new WP_Query( array( 'category_name' => 'past-event' ) );
So in your example case, this would become:
$loop = new WP_Query( array( 'post_type' => 'workshops', 'posts_per_page' => -1, 'category_name' => 'past-event' ) );
You can do a whole load of stuff as detailed in the code
If I understand you correctly, you're looking for something like this:
$args = array(
'post_type' => 'workshops',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'workshop-status',
'field' => 'slug',
'terms' => array( 'past-event'),
'operator' => 'IN'
),
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
//do your stuff
endwhile;
endif;
wp_reset_postdata();

Show category by latest post date

I am working in a project where I am using below code to limit number of posts in each category. But I can't figure out how to sort these categories by latest posts. My code is working fine but when I add argument in wp query array, it does not sort or it breaks. I want to order the category with most recent post (date).
Can anyone guide me?
<?php $terms=g et_terms( array( 'taxonomy'=>'category', 'hide_empty' => false, ) ); foreach($terms as $cat){ $cata_name = $cat->name; $term_id = $cat->term_id; ?>
<?php //echo '<h3>'.$catname[0]->cat_name.'</h3>'; ?>
<?php $catqueryy=n ew WP_Query ( 'cat='.$term_id. '&posts_per_page=5');$count=$ catqueryy->found_posts; ?>
You can add ORDER BY argument in wordpress as:
<?php
$args = array(
'post_type' => 'post', //your_post_type
'orderby' => 'date', //meta_value
'meta_query' => array(array('key' => 'cat','value'=>$term_id)),
'order' => 'DESC',
'posts_per_page' => 5,
);
$catqueryy = new WP_Query($args);
?>
You can explore the wordpress document: https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

Wordpress: WP_Query how to apply search criteria with custom post type

I have a custom post type, photo, and need to search for photos matching the title or description with the search keyword with various criteria: contains LIKE %$search_term%, starts with LIKE $search_term% etc. I have the following query, but this doesn't filter records according to $search_term. Please direct me to the right direction to embed this requirement with this query.
$search_term = $_GET['term'];
$search_criteria = $_GET['type'];
$loop = new WP_Query( array(
'post_type' => 'photo',
'posts_per_page' => 12,
'orderby'=> 'post_date'
));
Please be nice with me, I am a newbie in Wordpress and don't even know if I am asking a foolish question. But I am really stuck with it and need a solution. Any help will be appreciated a lot. Thank you everybody.
Add the "s" key to your existing arguments array:
$loop = new WP_Query( array(
'post_type' => 'photo',
'posts_per_page' => 12,
'orderby' => 'post_date',
's' => 'search_term'
));
Documentation can be found at: http://codex.wordpress.org/Class_Reference/WP_Query#Search_Parameter
Pass your search string here example like this ( 's'=>'test' )
<?php
/*pass your search string here example like this ( 's'=>'test' ) */
$args=array('s'=>'test','order'=> 'DESC', 'posts_per_page'=>get_option('posts_per_page'));
$query=new WP_Query($args);
if( $query->have_posts()):
while( $query->have_posts()): $query->the_post();
{
echo $post->post_title;
echo $post->post_content;
}
endwhile;
else:
endif;
?>

Categories