Add meta data from function for orderby in wordpress - php

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

Related

WP Sorting custom posts alphabetically by Title

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.

Wordpress short code to display all child page by parents ID

I'm working on a short code for loop through parents ID and display all child page, but I'm not quite sure how to make the loop and make it more custom.
Here's my code:
add_shortcode( 'home-page-listing', 'get_list' );
function get_list( $atts ) {
ob_start();
$atts = shortcode_atts( array(
'ids' => ''
), $atts );
if($atts['ids']!='')
{
$id_array = explode(',',$atts['ids']);
$homePages = new WP_Query( array(
'post_type' => 'page',
'post__in'=>$id_array,
'order' => 'ASC',
'orderby' => 'post__in',
'posts_per_page' => -1
) );
if ($homePages->have_posts()){?>
<div class="">
<?php while ( $homePages->have_posts() ) : $homePages->the_post(); ?>
//here's html template code
<?php endwhile;
wp_reset_postdata(); ?>
</div>
}
}
}
Right now I can use [home-page-listing id=1,2,3,4] to display all select page ID, but I would like to make like this:
[home-page-listing parentID=4]
loop through all child page and display to the font, instead go check all the page id to display.
Thanks!
it's simple used post_parent Arguments of WP_Query. Please check below example
$homePages = new WP_Query( array(
'post_type' => 'page',
'post_parent'=>$parentID,
'order' => 'ASC',
'orderby' => 'parent',
'posts_per_page' => -1
) );
For more information of WP_Query click here

Wordpress Find Index of Post in Query

I am trying to return the rank of a post within a custom query in wordpress. My code is currently
function post_rank($post_type,$meta_value,$post_id) {
$args = array (
'post_type' => $post_type,
'meta_key' => 'totalvotes',
'order' => 'DESC',
'orderby' => 'meta_value_num',
'post_status' => '\'submitted\'
);
$the_query = new WP_Query( $args );
$total_in_search = $the_query->found_posts;
$rank_in_search = array_search ($the_query,$post_id) +1 ;
$overall= $rank_in_search ."/".$total_in_search ;
wp_reset_postdata();
return $overall;
}
But i can't seem to find the post index. Any ideas would be appreciated.
According to the doc http://codex.wordpress.org/Class_Reference/WP_Query it must be
$current_post (available during The Loop) Index of the post currently being displayed.
I eventually used a loop as follows:
while ( $the_query->have_posts() ) : $the_query->the_post();
if ($post_id == get_the_ID())
{
$rank_in_search = $i; }
$i++;
endwhile;
Not the cleanest of approaches but will do for the time being. Any improvements always appreciated

Wordpress pagination is not working when don't preserve the initial query object

On a custom page template on my custom theme I have the below query. When I do it this way the pagination is working :
$myqueryname = $wp_query;
$mypost_args = array( 'post_type' => 'friends', 'orderby' => 'title', 'order' => 'ASC', 'posts_per_page' => 4, 'paged' => $paged);
$wp_query = new WP_Query($mypost_args);
while ( $wp_query->have_posts() ) : $wp_query->the_post();
Some html.....
endwhile;
previous_posts_link('Prev');
next_posts_link('Next');
But when I am using this way the pagination is not working :
$mypost_args = array( 'post_type' => 'friends', 'orderby' => 'title', 'order' => 'ASC');
$loop = new WP_Query( $mypost_args );
while ( $loop->have_posts() ) : $loop->the_post();
Some html.....
endwhile;
previous_posts_link('Prev');
next_posts_link('Next');
wp_reset_query();
I was wondering why is this happening ? What is the difference ? Sorry if my question is vague but this will help me to understand better the way wordpress is working and not just copy-paste codes from forums.
Thanks in advanced.
If you look into the code of the get_next_posts_link (function that next_posts_link calls) it has the following code:
global $paged, $wp_query;
if ( !$max_page )
$max_page = $wp_query->max_num_pages;
...
And this is the usage of next_posts_link
<?php next_posts_link( $label , $max_pages ); ?>
It looks to me that when you use custom query you need to specify $max_pages for pagination to work.
Try modifying your pagination code to this:
previous_posts_link('Prev', $loop->max_num_pages);
next_posts_link('Next', $loop->max_num_pages);

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