i am trying to create a small overview over the latest posts of my page. Everything works well with the "wp_get_recent_posts" query. Now i was trying to add some icons to the title, but it always shows me no result, as soon as i try to get the post_category of a post.
If tried to change the $args 'category' => to '1,2,3,4,...' but it didn't helped.
Any advice is highly appreciated. My code:
<?php
$args = array(
'numberposts' => 5,
'offset' => 0,
'category' => '',
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' =>'',
'post_type' => 'post',
'post_status' => 'draft, publish, future, pending, private',
'suppress_filters' => true
);
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
foreach($recent_posts as $post):
if($post['post_category'] == 1):
echo "<p><i class=\"fas fa-book\"> </i>{$post['post_title']}<br></p>";
elseif($post['post_category'] == 2):
echo "<p><i class=\"fas fa-desktop\"> </i>{$post['post_title']}<br></p>";
endif;
echo $post['post_category']; //Debug, no output.
echo $post['post_title']; //Debug, output: "example post"
echo $post['post_date']; //Debug, output: "2019-05-21"
endforeach;
?>
The post object does not have a post_category property: https://codex.wordpress.org/Class_Reference/WP_Post
You could use get_the_category function with the post ID (https://developer.wordpress.org/reference/functions/get_the_category/):
<?php
$args = array(
'numberposts' => 5,
'offset' => 0,
'category' => '',
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' =>'',
'post_type' => 'post',
'post_status' => 'draft, publish, future, pending, private',
'suppress_filters' => true
);
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
foreach($recent_posts as $post):
$categories = get_the_category($post['ID']);
foreach ($categories as $category):
if ($category->cat_name == 'firstcategory'):
//first category found
var_dump($category->cat_name);
endif;
if ($category->cat_name == 'secondcategory'):
//second category found
var_dump($category->cat_name);
endif;
endforeach;
endforeach;
?>
Related
I have this php code that is in one of my page templates events-template.php
I want to show it in on my homepage but have been unsuccessful. Any ideas on how this can be implemented.
Thanks!
<?php
JAMSESSION_SWP_put_the_title("div", get_the_title(), "post_title", "");
while (have_posts())
{
the_post();
$args = array(
'numberposts' => 100,
'posts_per_page' => 100,
'offset' => 0,
'category' => '',
'orderby' => array('event_date' => 'DESC', 'event_time' => 'DESC'),
'include' => '',
'exclude' => '',
'meta_key' => 'event_date',
'meta_value' => '',
'post_type' => 'js_events',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
'event_date' => array(
'key' => 'event_date'
),
'event_time' => array(
'key' => 'event_time'
)
),
'suppress_filters' => true
);
$myposts = get_posts( $args);
echo '<div id="postmeta_custom">';
echo ' <span class="post_cat">'; JAMSESSION_SWP_list_custom_terms_with_links('event_category', '', 'all'); echo "</span>";
echo '</div>';
/*events_content using $myposts var*/
if ('masonry' == JAMSESSION_SWP_get_events_view()) {
require_once(get_template_directory().'/views/events_content_masonry.php');
} else {
require_once(get_template_directory().'/views/events_content_list.php');
}
}
?>
What have you tried so far? Looking at the WordPress template hierarchy, a WordPress homepage could be using either front-page.php or home.php.
I've made a custom post type type called "Videos" and within that post type a custom category called "Crystal" while using this plugin(https://wordpress.org/plugins/video-thumbnails/ to generate the YouTube videos thumbnail into the post.
I'm trying to pull through all of the Crystal posts and only display the video thumbnail on the page with a permalink to the post.
Here is my code;
<div class="block" id="home-three">
<p>YouTube</p>
<?php
$args = array(
'post_type' => 'videos',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
'post_parent' => 0,
'tax_query' => 'crystal',
);
$count = 1;
?>
<?php $video_query = new WP_Query( $args ); ?>
<?php while ( $video_query->have_posts() ) : $video_query->the_post(); ?>
<div>
<a href="<?php the_permalink(); ?>">
<?php if( ( $video_thumbnail = get_video_thumbnail() ) != null ) { echo "<img src='https://wordpress.org/plugins/video-thumbnails/" . $video_thumbnail . "' />"; } ?>
</a>
</div>
<?php wp_reset_query(); ?>
</div>
According to documentation, the tax_query parameters accepts an array.
So your WP_Query arguments should be:
<?php
$args = array(
'post_type' => 'videos',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
'post_parent' => 0,
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'crystal'
)
)
);
?>
You could also use the Category Parameters:
<?php
$args = array(
'post_type' => 'videos',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
'post_parent' => 0,
'category_name' => 'crystal'
);
?>
The following is returning no results. I want to filter by category and multiple tags. Can you see what i am doing wrong?
$tags = array( "blah-blah", "sausage" );
$posts = get_posts( array(
'posts_per_page' => 3,
'offset' => 0,
'category' => $categoryID,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true,
'tag' => implode( ",", $tags )
) );
EDIT
This seems to work!:
$posts = get_posts( array(
'posts_per_page' => 3,
'offset' => 0,
'category' => $categoryID,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true,
'tag_slug__in' => $tags
) );
Try something like this :
$posts = get_posts( array(
'posts_per_page' => 3,
'offset' => 0,
'category__and' => $categoryID,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true,
'tag__in' => $tags
) );
Check if it works.
See below example to get posts by tags & category
global $wp_query;
$args = array(
'category__and' => 'category',
'tag__in' => 'post_tag', //must use tag id for this field
'posts_per_page' => -1); //get all posts
$posts = get_posts($args);
foreach ($posts as $post) :
//do stuff
endforeach;
I have kept a Prev-Next option in the single post page to navigate though the posts and this is what I'm using for the next button.
<?php echo get_permalink(get_adjacent_post(false,'',false)); ?>
But I can't figure out a way to link this same button to the very first posts when there are no more new posts to show. The reason is that the posts are used to show products.
Please note: To link Prev button in oldest post to the newest post I have used used this code.
<?php $next_page=get_permalink(get_adjacent_post(false,'',true));
$current_page=get_permalink();
if($next_page==$current_page){
$args = array( 'numberposts' => '1', 'category' => CAT_ID );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo get_permalink($recent["ID"]);
}
} else {
echo $next_page;
}
?>
add the args to query whatever posts are part of the products.
$posts_array = get_posts( $args );
get_permalink($posts_array[0]->ID); // First posts;
$args should be something like this (make sure it returns all the products posts):
$args = array(
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'ASC',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
In Wordpress I am trying to get all posts within multiple categories. Heres my code:
<?php
// Get categories
$menu = 'left-column-menu';
$cat_args = array(
'order' => 'ASC',
'orderby' => 'menu_order',
'post_type' => 'nav_menu_item',
'post_status' => 'publish',
'output' => ARRAY_A,
'output_key' => 'menu_order',
'nopaging' => true,
'update_post_term_cache' => false );
$cat_items = wp_get_nav_menu_items( $menu, $cat_args );
// Get categories posts
$args = array(
'posts_per_page' => '',
'offset' => 0,
'category' => $cat_items->ID,
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true );
$posts = get_posts($args);
print_r($posts);
?>
The first part is getting the categories from a menu, this is working fine. But its only getting the first 5 posts. I'm just wondering why this is?
Change:
// Get categories posts
$args = array(
'posts_per_page' => '',
To:
// Get categories posts
$args = array(
'posts_per_page' => -1,