Multiple post IDs from array in Wordpress - php

I want to display 3 specifics posts.
Problem : My posts IDs are from a previous array.
Result : It displays only the first one.
Function :
foreach($fav_author_list as $i => $item) {
$insert = get_user_favorites($item);
if (!is_array($insert[0])) {
$result = array_merge($result, $insert);
}
}
$algoid = implode(",", $result);
Result from $algoid (Post ID) = 865, 866, 877
I want to display the three posts.
$myarray = array($algoid);
$args = array(
'post__in' => $myarray,
);
// The Query
$the_query = new WP_Query( $args );

You don't have to implode your $algoid for the post__in. Since you're using implode, you're actually passing an array with a string for your query:
array('865, 866, 877'); // Items: 1
However, WP_Query is expecting an array with the ids, not as a string:
array(865, 866, 877); // Items: 3
Here's how it should be:
// Use your function to generate the array with the IDs
$algoid = array(865, 866, 877);
$args = array(
'post__in' => $algoid
);
For more information about WP_Query: https://codex.wordpress.org/Class_Reference/WP_Query
post__in (array) - use post ids. Specify posts to retrieve. ATTENTION If you use sticky posts, they will be included (prepended!) in the posts you retrieve whether you want it or not. To suppress this behaviour use ignore_sticky_posts.

Related

Custom Query > post__in => $page_id (multiple page not show / just show 1 page)

<?php
global $redux_demo;
$page_id = $redux_demo[input_id];
$query_page = new WP_Query(array(
'post_type'=>'page',
'post__in'=>array($page_id)
));
?>
its show just single page, not multiple pages.
var_dump($page_id); // output '192, 185, 188, 150' // include = 'punctuation'
if manual input = array(192, 185, 188, 150) show selected pages.
How can I fixed this problem $page_id ???
The post__in parameter expects an array of IDs, and you're passing it a string (see WP_Query - Post & Page Parameters for more details.
So, you need to convert $redux_demo[input_id] into an array first:
global $redux_demo;
// Convert input into an array, and remove whitespaces
$page_id = array_map( 'trim', explode(',', $redux_demo['input_id']) );
$query_page = new WP_Query(array(
'post_type' => 'page',
'post__in' => $page_id
));
Also, it seems you're missing some quotes here: $redux_demo[input_id]. Shouldn't it be $redux_demo['input_id'] instead?

Wordpress wp_query get result into variables

Currently I'm using get_posts to get the 10 latest posts, but the problem is that I don't get all information. For example I can't access the post category or author.
I would like to use WP_Query with get_the_title(), get_the_post_thumbnail, get_permalink() etc. The results have to be stored into variables.
My setup:
$latest_posts = get_posts( array(
'category_name' => 'Allgemein',
'posts_per_page' => 10,
"orderby" => "date",
"order" => "DESC"
) );
// Post 1 -- Category: Allgemein
if ( isset( $latest_posts[0] ) ) { // array zero-based index.
// $post1_category = $latest_posts[0]->post_category;
$post1_date = $latest_posts[0]->post_date;
$post1_title = $latest_posts[0]->post_title;
// $post1_tags = $latest_posts[0]->post_tags;
$post1_author = $latest_posts[0]->post_author;
$post1_content = $latest_posts[0]->post_content;
$post1_thumbnail = $latest_posts[0]->get_the_post_thumbnail;
}
// Post 2 -- Category: Allgemein
if ( isset( $latest_posts[1] ) ) {
// $post2_category = $latest_posts[1]->post_category;
$post2_date = $latest_posts[1]->post_date;
$post2_title = $latest_posts[1]->post_title;
// $post2_tags = $latest_posts[1]->post_tags;
$post2_author = $latest_posts[1]->post_author;
$post2_content = $latest_posts[1]->post_content;
$post2_thumbnail = $latest_posts[1]->get_the_post_thumbnail;
}
Post3-10
....
And in my index.php:
<div class="title">
<?php echo $post1_title?>
</div>
To get the categories, use the function wp_get_post_categories:
$post1_category = wp_get_post_categories($latest_posts[0]->ID);
To get the tags, use the function wp_get_post_tags:
$post1_tags = wp_get_post_tags($latest_posts[0]->ID);
WordPress has more functions to retrieve the post data using the post ID. Follow:
Get the Title
Get the post thumbnail URL
Your variable $latest_posts[X] is a WP_Post Object, wich contains some data. See the full data list in the WP_Post Reference.

Get Wordpress post title in array

I want to have Wordpress post title in array.
I have my post titles in my custom post type as names of people with their surnames. I want to display my posts based alphabetically on their surnames and store it in an array. How do I do this in the loop?
You can retrieve the posts for your custom post type using WP_Query, and then run through each of them to get the titles.
// just get IDs rather than whole post object - more efficient
// as you only require the title
$post_ids = new WP_Query(array(
'post_type' => 'custom_post_type_name', // replace with CPT name
'fields' => 'ids',
'orderby' => 'meta_value',
'meta_key' => 'surname_field_name' // replace with custom field name
));
$post_titles = array();
// go through each of the retrieved ids and get the title
if ($post_ids->have_posts()):
foreach( $post_ids->posts as $id ):
// get the post title, and apply any filters which plugins may have added
// (get_the_title returns unfiltered value)
$post_titles[] = apply_filters('the_title', get_the_title($id));
endforeach;
endif;
Using WP_Query has the benefit that it does not alter the main loop on your page, and you can get the posts in the order that your require by using orderby along with the name of the custom field which contains the surname.
You can create an array outside the loop, then get the array filled up with the names, than sort the array.
<?php
// the array for the names
$name_array = array();
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
$name_array[] = $post->title;
//
// Post Content here
//
} // end while
if ( sizeof( $name_array ) > 0 ) {
sort( $name_array );
} // end if for sizeof()
} // end if
?>
If you cannot create the array outside The Loop, than you may move it under if ( have_posts() ) {.
Important note: this solution only contains the names in your current loop, so if your query does not hold all the posts, or is offset / paged, etc. then the array will not get all the names you have in your custom post type. If you would like to have all the names in the array and your loop query does not hold all the posts, then you have to query again - just for the titles (names).

Add incremental parameter value to each post in wordpress query loop

I'm trying to combine several loops into one and sort the final results by relevance.
For the former part, I did this:
// set the variables
$author_id = get_the_author_meta('ID');
$tags_id = wp_get_post_tags($post->ID);
$first_tag = $tags_id[0]->term_id;
$categories_id = wp_get_post_categories($post->ID);
// loop for same author
$by_author = new WP_Query (array(
'author' => $author_id,
'posts_per_page' => '5'
));
// add ids to array
if ($by_author->have_posts()) {
while ($by_author->have_posts()) {
$by_author->the_post();
$add[] = get_the_id();
}
}
// loop for same tag
$by_tag = new WP_Query(array(
'tag__in' => $first_tag,
'posts_per_page' => '5'
));
// add ids to array
if ($by_tag->have_posts()) {
while ($by_tag->have_posts()) {
$by_tag->the_post();
$add[] = get_the_id();
}
}
// loop for same category
$by_category = new WP_Query(array(
'category__in' => $categories_id,
'posts_per_page' => '5'
));
// add ids to array
if ($by_category->have_posts()) {
while ($by_category->have_posts()) {
$by_category->the_post();
$add[] = get_the_id();
}
}
// loop array of combined results
$related = new WP_Query(array(
'post__in' => $add,
'post__not_in' => array($post->ID),
'posts_per_page' => '10',
'orderby' => $weight[$post->ID],
'order' => 'DESC'
));
// show them
if ($related->have_posts()) {
while ($related->have_posts()) {
$related->the_post();
// [template]
}
}
This is working nicely combining the loops into one. For the latter part, what I'm trying to do next is to add an incremental "weight" value to each post as they come up so as to later sort them with something like 'orderby' => $weight,.
For example, if a post comes up in "same author" it gets 3 points, if another one comes up in same tag it gets 2 points and so on. If it comes up in more than one loop, it should get the combined points i.e. 3+2+1=6 hence be boosted to the top of the final query.
I have tried to add a counter to each preliminary loop, like $weight = +3 etc, but this only adds everything up for every post, not individually.
I also tried inserting something like this at the end of each preliminary loop...
$weight = 0;
if ($by_author){
foreach ($by_author as $post){
setup_postdata($post);
$weight = +10;
add_post_meta($post->ID, 'incr_number', $weight, true);
update_post_meta($post->ID, 'incr_number', $weight);
}
}
... and this to the final one
echo get_post_meta($post->ID,'incr_number',true);
But it still doesnt do it right. It assigns a global value, while I want them to be different depending on the actual main posts one is reading.
So is there a way to do this?
If I understand your question right, I think your last solution was close. Instead of a global $weight parameter, however, I think you need to build an array of $weights that's unique to each post:
$weights[$post.id] += 10;
Then, you could sort that array and get your most heavily weighted post IDs from there.

Wordpress WP_Query category__in with order

Hi I'm trying to make a query to get posts from a specific categories like this:
$args = array('category__in' => array(8,3,12,7));
$posts = new WP_Query($args);
But I need the posts to be displayed in that specific order ( cat id 8 first, then 3, etc ), I can't get it to work properly, posts are displayed according to ASC or DESC names.
Any Help?
You can sort by post__in to use the order of the input values, but with category__in it is a many to many relationship, so using it order by is considerably more difficult and is not supported as far as I know. Also note that WP_Query() does not return an array of posts.
If you have your specific set of ordering rules you can take the results from get_posts() using the category argument, and then use a custom sorting function to order the results using usort() and get_categories().
// function used by usort() to sort your array of posts
function sort_posts_by_categories( $a, $b ){
// $a and $b are post object elements from your array of posts
$a_cats = get_categories( $a->ID );
$b_cats = get_categories( $b->ID );
// determine how you want to compare these two arrays of categories...
// perhaps if the categories are the same you want to follow it by title, etc
// return -1 if you want $a before $b
// return 1 if you want $b before $a
// return 0 if they are equal
}
// get an array of post objects in the 'category' IDs provided
$posts = get_posts( array( 'category' => '8,3,12,7' ) );
// sort $posts using your custom function which compares the categories.
usort( $posts, 'sort_posts_by_categories' );
To my knowledge the best approach here is to make 4 separate queries.

Categories