Wordpress Find Index of Post in Query - php

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

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.

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

Exclude all sub categories from wp_query

I've been searching high and low for and answer to this, but I'm not actually sure it's possible!
I have a WP_Query that pulls posts from almost everything, however, I wish to exclude a specific category and/or all it's sub categories.
Searching around people are yet to find a solution for this.
Here's my query so far:
$args = array(
'post_type' => 'sell_media_item',
'cat' => -98,
'orderby' => 'desc',
'paged' => $paged,
'posts_per_page' => 20
); ?>
<?php $loop = new WP_Query( $args ); ?>
I thought just excluding cat 98 would grab all the sub categories too, but apparently not.
I've tried using:
category__not_in, depth=0, parent=0 and even an adaptation of this, with no luck.
Any ideas?
[EDIT]
I'm using a custom taxonomy called Collections, so putting 'collection' => 'vip' into the query means it will only show this collection. I'm thinking if there's a way of reversing this so it excludes the collection instead?
As it's not possible to list all of the categories that will appear here as they will be changing all of the time.
[EDIT 2]
After the discussion in the comments below, here's the updated code.
$ex = array(
'taxonomy' => 'collection',
'child_of' => 98,
'hide_empty' => 0
);
$categories = get_categories($ex);
$categoriesToExclude = array();
foreach ($categories as $category) {
$categoriesToExclude[] = $category->cat_ID;
}
echo('<pre>'); var_dump($categories);
$args = array(
'post_type' => 'sell_media_item',
'category__not_in' => $categoriesToExclude,
'orderby' => 'desc',
'paged' => $paged,
'posts_per_page' => 20
); ?>
<?php echo('<br /><pre>'); var_dump($args); ?>
<?php $loop = new WP_Query( $args ); ?>
I would get the list of all sub categories with get_categories() and then build a 'cat' exclusion array based on the results.
$args = array('parent' => 98);
$categories = get_categories($args);
$categoriesToExclude = array();
foreach ($categories as $category) {
$categoriesToExclude[] = $category->cat_ID;
}
$args = array(
'post_type' => 'sell_media_item',
'category__not_in' => $categoriesToExclude,
'orderby' => 'desc',
'paged' => $paged,
'posts_per_page' => 20
); ?>
<?php $loop = new WP_Query( $args ); ?>
This is just an example, you may have to modify it slightly to fit your needs.
So!
It appears I was trying to do the impossible. I couldn't get this script working for the life of me. So I tried a different angle. Instead of excluding a custom taxonomy and its terms, I decided to move all of my other terms into a parent term and just called that instead.
Here's the code if anyone's interested...
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'sell_media_item',
'taxonomy' => 'collection',
'term' => 'clubs',
'orderby' => 'desc',
'paged' => $paged,
'posts_per_page' => 20
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) : while ($loop->have_posts()) : $loop->the_post(); ?>
I wrote my own function in order to exclude subcategory posts from the loop, using tips from the above post and elsewhere.
In my theme archive.php file, above the loop, I list the subcategories (optional):
<?php
$current_cat = get_queried_object();
$args = array( 'parent'=>$current_cat->term_id, 'child_of' => $current_cat->term_id, );
$categories = get_categories( $args );
foreach($categories as $category) { ?>
<h2><?php echo $category->name ;?></h2>
<p> etc....</p>
<?php } ?>
In my functions.php file, I've added the following custom function using pre_get_posts:
add_action( 'pre_get_posts', 'main_query_without_subcategory_posts' );
function main_query_without_subcategory_posts( $query ) {
if ( ! is_admin() && $query->is_main_query() ) {
// Not a query for an admin page.
// It's the main query for a front end page of your site.
if ( is_category() ) {
//Get the current category
$current_category = get_queried_object();
//get the id of the current category
$current_cat_id = $current_category->term_id;
//find the children of current category
$cat_args = array( 'parent'=>$current_category->term_id, 'child_of' => $current_category->term_id, );
$subcategories = get_categories( $cat_args );
//Get a list of subcategory ids, stick a minus sign in front
$subcat_id = array();
foreach($subcategories as $subcategory) {
$subcat_id[] = " -". $subcategory->term_id;
}
//join them together as a string with a comma seperator
$excludesubcatlist = join(',', $subcat_id);
//If you have multiple parameters, use $query->set multiple times
$query->set( 'posts_per_page', '10' );
$query->set( 'cat', ''.$current_cat_id.','.$excludesubcatlist.'' );
}
}
}
Then in the archive.php, below the subcategories, I've added the regular WordPress loop which is now being modified by the above function:
<?php while (have_posts() ) : the_post(); ?>
<h2><?php the_title();?></h2>
<p> etc....</p>
<?php endwhile;?>
Though the WordPress codex says that using "category__in" will exclude posts from subcategories, that didn't work for me and subcategory posts were still showing.
https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
https://developer.wordpress.org/reference/hooks/pre_get_posts/

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