How to get latest post id in functions.php Wordpress - wordpress

Can anyone help me to get latest post id in Wordpress functions.php file.
I think it must be something like
global $wp_query;
$thePostID = $wp_query->post->ID;
But cant figure out what next.
Thanks

Using wp_get_recent_posts:
$recent_posts = wp_get_recent_posts( array( 'numberposts' => '1' ) );
$thePostID = $recent_posts[0]['ID'];

Related

Custom WP_Query for search

I'm trying to make a custom WP_Query for a search results page.
With the following code, the page always displayed all posts regardless:
<?php
$args = array(
'posts_per_page' => '-1',
);
$query = new WP_Query( $args );
?>
So I've added the search Query to the $args, but this always returns no results - where is this going wrong?
<?php
$search_query = get_search_query();
echo $search_query;
$args = array(
'posts_per_page' => '-1',
's' => $search_query
);
$query = new WP_Query( $args );
?>
1) You can use the templatesearch.php and searchform.php as your starting points. Creating a Search Page Codex
2) As far as the custom query goes, you can use pre_get_posts hook to test if you're on a search page, then you get $_GET your values, edit your query accordingly. Action Reference - pre_get_posts
There are tons of tutorials online and questions on this exchange to help you out. Some are Simple and others are more Complex. You'll have to do some real research to accomplish this. Hope it helps!
I have solved this using pre_get_posts - I'm still intrigued as to why using the WP_Query method doesn't work, but here's what I'm now using:
function search_filter($query) {
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_search) {
$query->set('posts_per_page', '-1');
}
}
}
add_action('pre_get_posts','search_filter');

Using Customizer to define category_name in Wordpress

I am trying to use the Wordpress Customizer to allow the client to input the category names for post that will appear on the homepage. The design calls for two columns of posts, with definable categories for each column.
My hope is that I could do something like:
if( get_theme_mod( 'column_1_category') != "" );
$args = array(
'category_name' => echo get_theme_mod( 'column_1_category'); ),
'posts_per_page' => 2
);
I have already defined the column_1_category in customizer.php, and it works great by itself, but I would like whatever category is typed into the customizer to define the category_name in the $args = array( code.
The 'category_name' = > echo . . is the line that keeps giving me errors. I am assume, if this even works, that I am missing some code in there?
I was hoping this would be a quick and easy way to define the Category, but not sure if this would even work?
Thanks.
Nevermind, i figured it out. All I needed to do was:
$category_2_name = get_theme_mod( 'column_2_content');
$args = array(
'category_name' => ($category_2_name),
'posts_per_page' => 2
);

How to have the title of the last post in a static page?

I have a page for posts and a different static home page, as well as other common pages, eg About, Sponsors.
I am trying to have in the header of all pages the name of the site and after the title of the last post. I have tried different functions (the_title(), get_the_title()). It works well in the post page, but for all the remaining I get the title of the page ("About" or "Home" or "Sponsors").
I get that the functions I used so far might only work inside the loop, but was wondering if there's some global way to have the title of the last post available everywhere.
I got to: $posts_page = get_option( 'page_for_posts' ) to get the id of the post page and was trying to find a way from there to get the title of the last post, but no success so far.
Is there a simple way to do this?
Try it:
echo current( wp_get_recent_posts( array('numberposts' => 1, 'post_status' => 'publish') ) )['post_title'];
Do you mean the latest/most recent post?
Would this work for you?
<?php
$args = array(
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'DESC'
);
$post = get_posts( $args );
$latest_post_title = $post[0]->post_title;
echo $latest_post_title;
?>
Or maybe this is a bit more light weight, not sure but they both do pretty much the same thing.
<?php
$args = array(
'numberposts' => 1,
);
$recent = wp_get_recent_posts($args);
$last_post_title = $recent[0]['post_title'];
echo $last_post_title;
?>

How to hook in a custom query to the loop with Wordpress

I want to create a loop and query posts by their author role. And display the results of a search term based on the authors role.
I've tried creating a function to alter the query's where clause:
$ids = get_users(
array(
'role' => 'administrator' ,
'fields' => 'ID'
)
);
$query = new WP_Query(
array(
'author__in' => $ids,
)
);
// If the query has data
if($query->have_posts() ) :
// Post loop
while ($query->have_posts() ) :
// Setup post data
$query->the_post();
?>
<!-- Do HTML markup and template tags here, eg. the_content(), the_title() etc.. -->
<h1>You're a post from administrator - <?php the_title(); ?></h1>
<?php get_template_part( 'template-parts/content', 'search' ); ?>
<?php
endwhile;
// End "If the query has data"
endif;
I'm trying to add a WP_Query to the loop but this is where I get stuck with the results not getting filtered by role so I'm fairly certain I must be implementing this wrong - this is the first time I've tried to do something like this so sorry if it's a dump question but I can't find an answer to my question so if anyone can point me in the right direction that would be amazing!
Any advice welcome, thank you!
You can try the posts_where
hook.
UPDATE:
Why don't you use the default WP_Query author arg instead hook.
You query will be :
$ids = get_users(
array(
'role' => 'administrator' ,
'fields' => 'ID'
)
);
$query = new WP_Query(
array(
'author__in' => $ids,
)
);

Wordpress twitter bootstrap image carousel from specific category

I'm using the Bootstrap theme from 320press and currently struggling with the image slider carousel. It displays the posts of every featured image on the site, when I just want a specific category. I guess it's the following code I have to tweak som way...?
<?php
global $post;
$tmp_post = $post;
$show_posts = of_get_option('slider_options');
$args = array( '2' => $show_posts ); // set this to how many posts you want in the carousel
$myposts = get_posts( $args );
$post_num = 0;
foreach( $myposts as $post ) : setup_postdata($post);
$post_num++;
$post_thumbnail_id = get_post_thumbnail_id();
$featured_src = wp_get_attachment_image_src( $post_thumbnail_id, 'wpbs-featured-carousel' );
?>
Try replacing the $args for get_posts() with the following. You'll need to put in the slug of the category that you want to show. Since I don't know exactly how this plugin works, I can't guarantee it'll work as expected.
$args = array(
'posts_per_page' => $show_posts,
'category_name' => 'your_category_slug'
);
You can see a complete list of the arguments available for get_posts() on the WP_Query page of the WordPress codex: http://codex.wordpress.org/Class_Reference/WP_Query.
I'm a little confused by the counter that your code is setting. It seems like setting the number of posts to fetch as an argument to get_posts() would make a lot more sense.

Resources