How to render formatted post content in widget in Wordpress - php

I am new on wordpress and try to make a widget which will only show the last post of particuler category.
TO do this I have given some settings in admin to select category for which admin wants to display last post (post of last ID).
I am using the below code to in widget to display post content:
$query_arguments = array(
'posts_per_page' => (int) 1,
'post_type' => 'post',
'post_status' => 'publish',
'cat' => (int) $settings['category'],
'order' => 'DESC',
'orderby' => (($settings['display_by'] == 0) ? 'ID' : 'date')
);
$posts_query = new WP_Query( $query_arguments );
if ($posts_query->have_posts()) {
$posts = $posts_query->get_posts();
$post = $posts[0];
echo $post->post_content;
}
But the above code is showing content in one paragraph or you can say that without format. I have done lot of search and found that, I need to apply "the_content" filter to format the content. So I have done the same as below code:
if ($posts_query->have_posts()) {
$posts = $posts_query->get_posts();
$post = $posts[0];
$content = apply_filters('the_content', $post->post_content);
echo $content;
}
Now the above changes is returning the null string. I have google lot of things but everyone is saying that to use apply filter or use the_content() function. I have tried both solutions but nothing happening.
Can anyone please share the solution for this problem?

please check whether you are getting any text in $post->post_content
by echo $post->post_content

All you need to do is,
$query_arguments = array(
'posts_per_page' => (int) 1,
'post_type' => 'post',
'post_status' => 'publish',
'cat' => (int) $settings['category'],
'order' => 'DESC',
'orderby' => (($settings['display_by'] == 0) ? 'ID' : 'date')
);
$posts_query = new WP_Query( $query_arguments );
while ($posts_query->have_posts()) {
$posts_query->the_post();
echo get_the_content();
}

Related

Custom Post Type Get Url Loop ShortCode

I have a PHP script here I can't seem to wrap my head around, im basically trying to create a shortcode that displays the next custom post type url, then when the current post is also the last post, it loops back pulling the url to the first post.
everything works except for when you are on the final post, the shortcode displays the ID of the current post (final post) instead of the url of the first post, I am assuming it has something to do with " 'fields' => 'ids' " but i have no idea as I don't write php:
function next_post_url() {
$next_post = get_next_post();
if(!$next_post){
$args = array(
'numberposts' => 1,
'post_type' => 'portfolio',
'order' => 'ASC',
'fields' => 'ids'
);
$first_post = get_posts( $args );
$next_post_url = $first_post[0];
}
else{
$next_post_url = get_next_post()->post_title;
}
return get_permalink($next_post->ID);}add_shortcode( 'next_post_url', 'next_post_url' );
Here you go please modify your php code as shown below.
function next_post_url() {
$next_post = get_next_post();
if (!$next_post) {
$args = array(
'numberposts' => 1,
'post_type' => 'portfolio',
'order' => 'ASC',
'fields' => 'ids'
);
$first_post = get_posts($args);
$post = $first_post[0];
} else {
$post = $next_post;
}
return get_permalink($post);
}
add_shortcode('next_post_url', 'next_post_url');

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 ad_filter to title

i have problem to ad tiitle to title :P
I just have created custom post type. Now i want Get random title from element from custom post type and write it after POST (standard post) here is my code:
function pobierajslowa() {
$args=array(
'orderby' => rand,
'post_type' => 'slowa_kluczowe_wpisu',
'posts_per_page' => 1,
'caller_get_posts'=> 1
);
$queryObject = new WP_Query($args);
while ($queryObject->have_posts()) {
$queryObject->the_post();
return get_the_title();
}
}
function theme_slug_filter_the_content( $title ) {
$custom_content = $title;
$custom_content .= ' '.pobierajslowa().'';
return $custom_content;
}
add_filter( 'the_title', 'theme_slug_filter_the_content' );
After when i use this, i get 503 error :O What i have do wrong?
'orderby' => rand,
Should be...
'orderby' => 'rand', // a string
Make sure to double check syntax. Turning on WP_Debug should tell you where the issues are.

Limit posts per page after using array_merge in Wordpress

I have two post types I want to display, Posts and then a Custom Post Type called 'Notes'. I want to query both of these and display them together. I've currently got it working using array_merge.
I want to create a new query so I can choose how many posts to display per page and also get pagination working. I've tried various different things to limit the amount of posts displayed but can't seem to crack it.
Here is my code:
$q1_args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'post_status' => 'publish'
);
$q1_posts = get_posts( $q1_args );
// get the posts for the second query
$q2_args = array(
'post_type' => 'notes',
'posts_per_page' => -1,
'post_status' => 'publish'
);
$q2_posts= get_posts( $q2_args );
// Merge the post arrays together, and sort by date using the order_by_date function
$final_posts = array_merge( $q1_posts, $q2_posts );
usort( $final_posts, 'order_by_date' );
// Loop over the posts and use setup_postdata to format for template tag usage
foreach ( $final_posts as $key => $post ) {
$post_type = $post->post_type;
setup_postdata( $post );
//DO STUFF
}
Any thoughts on how I can limit posts per page and get pagination working?
Is there any particular reason this can't be done like this?
$args=array(
'post_type' => array('post', 'notes'),
'posts_per_page' => 15, //or any other number you want per page
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'paged' => (( get_query_var('page') ) ? get_query_var('page') : 1)
);
$posts=get_posts($args);
if ($posts->have_posts())
{
while ($posts->have_posts())
{
$posts->the_post();
//DO STUFF
}
//add pagination here
}
else
{
// no posts found
}
wp_reset_postdata();

Wordpress Jetpack Featured Content Tag ID

I'd like to be able to call the user-selected tag ID for the Jetpack Featured Content module, but I can't find a function or an object that holds that value.
I'm basically trying to filter featured posts out of a WP_Query with these arguments:
$args = array(
'ignore_sticky_posts' => 1,
'posts_per_page' => $recent_posts_count,
'tag__not_in' => array(
[HERE IS WHERE I WANT PHP TO TELL ME THE TAG ID]
),
'post_type' => array(
'post'
)
);
Check This one
<?php
global $post;
$tags = get_tags($post->ID);
$tagids = array();
foreach($tags as $tag) $tagids[] = $tag->term_id;
$args = array(
'ignore_sticky_posts' => 1,
'posts_per_page' => $recent_posts_count,
'tag__not_in' => $tagids,
'post_type' => array(
'post'
)
);
?>
Thanks
I know this is pretty late, but I found this post while looking for the same thing, even posted in the WordPress support forum, but figured it out eventually:
$featured_options = get_option( 'featured-content' );
$featured_name = $featured_options[ 'tag-name' ];

Categories