This is what i print out.
$args = array(
'category' => 'Animation',
'numberposts' => 8
);
$posts_array = get_posts( $args );
echo json_encode($posts_array);
this is the result: (there are 8 identical JSON lists like this, just showing 1 for convenience.)
{"ID":554,"post_author":"1","post_date":"2012-12-28 19:17:43","post_date_gmt":"2012-12-28 19:17:43","post_content":"The race to space is on. As nations compete, we follow the progress of a single chimpanzee that's been recruited into the Space Program. His results might prove influential for the better of all mankind. ...will he be up for the challenge","post_title":"Alpha","post_excerpt":"","post_status":"publish","comment_status":"open","ping_status":"open","post_password":"","post_name":"alpha","to_ping":"","pinged":"","post_modified":"2012-12-28 19:17:43","post_modified_gmt":"2012-12-28 19:17:43","post_content_filtered":"","post_parent":0,"guid":"http://localhost/Wordpress%203.4.2/?p=554","menu_order":0,"post_type":"post","post_mime_type":"","comment_count":"0","filter":"raw"
but i just want to send the id and the post_content. i keep on getting null values and cant figure out why.
Just filter by the fields you need: (id and the post_content or title and permalink)
$args = array(
'category' => 'Animation',
'numberposts' => 8
);
$posts_array = get_posts($args);
$send_array = array();
foreach ($posts_array as $key => $value)
{
$send_array[$key]["ID"] = $value->ID;
$send_array[$key]["post_content"] = $value->post_content;
}
echo json_encode($send_array);
exit;
Related
Fairly new to wordpress, but Im trying to do what should be fairly straight forward. I want to get a list of all "departments" (a custom taxonomy). and then all the posts in those departments. A post can be in more than one department.
All the examples I have found seems to get the departments then loop through them and get the posts, but this seems like an inefficient way to query the DB.
So my end result would hopefully look like this:
[
"Department 1" => [
0 => post 1,
1 => post 2,
2 => post 3
],
"Department 2" => [
0 => post 1,
1 => post 2,
2 => post 4
],
]
Note, some posts appearing under multiple departments.
Can anyone point me in the right direction?
Thanks in advance
You use the posts_clauses filter hook and modify the SQL query based on your requirements. check below code.
function orderby_tax_grouped_clauses( $clauses, $wp_query ) {
global $wpdb;
$taxonomy = 'your_custom_taxonomy';
$clauses['join'] .= "
LEFT OUTER JOIN {$wpdb->term_relationships} AS rel2 ON {$wpdb->posts}.ID = rel2.object_id
LEFT OUTER JOIN {$wpdb->term_taxonomy} AS tax2 ON rel2.term_taxonomy_id = tax2.term_taxonomy_id
LEFT OUTER JOIN {$wpdb->terms} USING (term_id)
";
$clauses['where'] .= " AND (taxonomy = '".$taxonomy."' OR taxonomy IS NULL)";
$clauses['groupby'] = "rel2.object_id";
$clauses['orderby'] = "GROUP_CONCAT({$wpdb->terms}.name ORDER BY name ASC) "; // 'ASC' OR 'DESC';
return $clauses;
}
add_filter( 'posts_clauses', 'orderby_tax_grouped_clauses', 10, 2 );
$args = array(
'post_type' => 'your_custom_post_type', // your CPT name
'posts_per_page' => -1
);
add_filter('posts_clauses', 'orderby_tax_grouped_clauses', 10, 2 );
$post_list = new WP_Query( $args );
remove_filter('posts_clauses', 'orderby_tax_grouped_clauses', 10, 2 );
and remove filter so it's only apply to your query not all query.
Please try with this code. May is useful for you. Thanks
$list_custom_taxonomy = get_terms('your_custom_taxonomy'); // your taxonomy name
foreach($list_custom_taxonomy as $custom_single_term) {
$args = array(
'post_type' => 'your_custom_post_type', // your CPT name
'tax_query' => array(
array(
'taxonomy' => 'your_custom_taxonomy', // your taxonomy name
'field' => 'slug',
'terms' => $custom_single_term->slug,
),
),
);
$post_list = new WP_Query($args);
if($post_list->have_posts()) {
echo '<h1>'.$custom_single_term->name.'</h1>';
while($post_list->have_posts()) { $post_list->the_post();
echo '<h6>'.get_the_title().'</h6>';
}
}
wp_reset_postdata();
}
I have a piece of code for displaying related posts by the post tag, but I don't know how to call posts in theme,
so I have this code which I have to put in my theme functions file:
function exe_get_related_posts_by_common_terms( $post_id, $number_posts = 0, $taxonomy = 'post_tag', $post_type = 'post' ) {
global $wpdb;
$post_id = (int) $post_id;
$number_posts = (int) $number_posts;
$limit = $number_posts > 0 ? ' LIMIT ' . $number_posts : '';
$related_posts_records = $wpdb->get_results(
$wpdb->prepare(
"SELECT tr.object_id, count( tr.term_taxonomy_id ) AS common_tax_count
FROM {$wpdb->term_relationships} AS tr
INNER JOIN {$wpdb->term_relationships} AS tr2 ON tr.term_taxonomy_id = tr2.term_taxonomy_id
INNER JOIN {$wpdb->term_taxonomy} as tt ON tt.term_taxonomy_id = tr2.term_taxonomy_id
INNER JOIN {$wpdb->posts} as p ON p.ID = tr.object_id
WHERE
tr2.object_id = %d
AND tt.taxonomy = %s
AND p.post_type = %s
GROUP BY tr.object_id
HAVING tr.object_id != %d
ORDER BY common_tax_count DESC" . $limit,
$post_id, $taxonomy, $post_type, $post_id
)
);
if ( count( $related_posts_records ) === 0 )
return false;
$related_posts = array();
foreach( $related_posts_records as $record )
$related_posts[] = array(
'post_id' => (int) $record->object_id,
'common_tax_count' => $record->common_tax_count
);
return $related_posts;
}
and now I want to call 10 posts from the above code in my single.php that can show post title and post thumbnail with the link to every post.
try something like that:
$query = new WP_Query(array(
'post_status' => 'publish',
'post_type' => 'your-posttype', // or 'any'
'tag_slug__in' => 'your-tag',
'order' => 'DESC',
'posts_per_page' => 10
));
Then you will get latest 10 posts
This function is very useful, people continually looking for ways to get related posts
What this function does:
The code you are using is a working function that is returning an array of posts related to current post depending on common terms.
How this function works:
You can call the function from your single.php file and as input you have to provide 4 info. The current post ID, the number of related posts to return, the post_term to filter the related posts (post_tag) and at last the post_type.
Example function call: exe_get_related_posts_by_common_terms(get_the_ID(), 10, 'post_tag', 'post')
The above example will return 10 post ids as array based on current post ID and the tag
- Returning the post array and loop through ID's
Now we only need a custom wp_query in order to loop through the array and format the output.
Example wp_query:
$args = array(
'post_type' => 'post',
'post__in' => $related_post
);
// The Query
$related_query = new WP_Query( $args );
Result:
Full working example returning the post titles in an unordered list:
<?php
$cpid = get_the_ID(); // get current post id
$related_posts = exe_get_related_posts_by_common_terms($cpid, 10, 'post_tag', 'post');
$posts_array = array_column($related_posts, 'post_id'); // new single dimension array with the ID's
$args = array(
'post_type' => 'post',
'post__in' => $posts_array
);
// The Query
$related_query = new WP_Query( $args );
if ($related_query->have_posts()) : ?>
<ul>
<?php while ($related_query->have_posts()) : $related_query->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
You can now set the look and fill as you like
For custom post type "bedrijf", i created a possibility where visitors can leave a rating. If they do, the meta key and value is added and saved. If there is more than one, the average is calculated and the value field is updated.
However, most custom posts don't have a meta key and value yet and i want them to have one with value "0".
The code I got so far:
function create_metadata(){
$args = array(
'post_type' => 'bedrijf',
'posts_per_page' => -1,
);
$posts = get_posts($args);
foreach ( $posts as $post ) {
$av = get_post_meta($post->ID, 'averagerating', true);
if (!isset($av)){
add_post_meta( $post->ID, 'averagerating', '0' );
}
}
}
add_action('init','create_metadata');
But so far this didn't work.
I found the problem, i had to use "new query" like this:
function create_metadata(){
$query = new WP_Query(array(
'post_type' => 'bedrijf',
'posts_per_page' => -1,
));
while ($query->have_posts()) {
$query->the_post();
if(!null == get_post_meta( get_the_ID(), 'averagerating', true )){
add_post_meta( get_the_ID(), 'averagerating', '0' );
}
}
How can I get a serial number of current post in category?
For ex. I have a category Cars with 4 posts in it. When I open some post I want to see navigation like this: Post 3 of 4 [<<] [>>]
Most straightforward way is querying the posts in the category, like this:
// WP_Query arguments
$args = array (
'category_name' => 'cars',
'posts_per_page' => '-1',
'order' => 'DESC',
'orderby' => 'date',
);
// The Query
$query = new WP_Query( $args );
Then you can get the number of posts with
// $query->found_posts gives the number of posts the query has found
// with the parameters you set
echo $query->found_posts;
And you can count up the post you display:
$count = 0;
foreach ( $query->posts as $count_post ) {
$count++;
// assuming you are inside The Loop
if ( get_the_ID() == $count_post->ID ) {
break;
}
}
// now you can get the "serial number" of the post
echo $count;
This might not be the most "WP way" of doing it, but it should work. :)
On our site we currently have 'work' posts that we create and then associate with an author and genre post type.
The overall goal is:
When we view an author or genre post we want to list all the work posts that we have associated/related with that certain author/genre.
We are using the following code which seems to have us half way...
<?php $args = array(
'numberposts' => -1,
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'related_posts',
'value' => $post->id
)
)
);
$posts_array = get_posts( $args );
var_dump($posts_array);
if( $posts_array ) {
echo '<ul>';
foreach( $posts_array as $related ) {
echo '<li>';
echo '' . $related->post_title . '';
echo '</li>';
}
echo '</ul>';
}
?>
However the 'value' field in the array doesn't work. It technically should be passing the id of the current post (author or genre) and selecting the related content. When we remove this from the array it does bring all the posts in whether they are related or not.
In summary, we think that the 'value' problem may be the key in resolving the issue as that is what should be filtering the posts.
Thanks in advance
did you try $post->ID? (uppercase not lowercase)