Wordpress wp_get_recent_posts always order by title - php

I want to change the defaults of the "wp_get_recent_posts" Function to get and order by title name.
I know how to do it with
$args=array('orderby'=> "title",'order'=> "ASC");
$recent_posts = $this->wp_get_recent_posts($args);
My question is how can i add these $args to the function wp_get_recent_posts from child-theme functions.php file with a hook/filter without editing other files.
Thanks.

Use function wp_get_recent_posts
<?php
$args = array(
'numberposts' => 10,
'orderby' => 'post_title',
'order' => 'ASC',
'post_type' => 'post',
'post_status' => 'publish'
);
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
?>

AFAIK there is no direct way to set a new "default" for the function.
The only feasable way I see is to write your own wrapper-function:
function wp_get_recent_posts_title( $args = array(), $output = ARRAY_A ) {
$defaults = array( 'orderby' => 'title' );
$args = wp_parse_args( $args, $defaults );
return wp_get_recent_posts( $args, $output );
}
You call this function just like the original, it will forward all parameters, with the exception that 'orderby' will be 'title' when it is not set explicitly.
Of course this will only work where you call it, it will not change any other calls to the original function.

Related

Custom Shortcode w/ Custom $tax_query Attribute

I'm trying to create a custom shortcode in Wordpress that will let me place an attribute to specify a category and query within a custom post type called "case_studies". The taxonomy itself is called "case_study_categories" and I've set the default to be "furniture-interior", as you can see.
What I need is for the shortcode to look something like this:
[get_project_gallery order="ASC" tax_query="flooring"]
Ideally, the shortcode attribute I've placed would override the default of "furniture-interior" and instead use "flooring" to display the appropriate posts. Actually, I would think I don't even really need a default defined at all (so it would just get all the case_studies posts if no attribute is specified) but I can't get that to work either.
BTW, the attribute for order works exactly the way I would want - it overrides the defined default. I just can't figure out how to get this done with the $tax_query.
Any help on this would be very much appreciated. Thanks in advance all!
Here's my code:
function project_gallery_function($atts, $content = null) {
extract(shortcode_atts(array(
'order' => 'DESC',
'orderby' => 'date',
'tax_query' => array(
array(
'taxonomy' => 'case_study_categories',
'field' => 'slug',
'terms' => 'furniture-interior'
)
)
), $atts));
$args = array(
'post_type' => 'case_studies',
'post_status' => 'publish',
'posts_per_page' => '4',
'order' => $order,
'orderby' => $orderby,
'tax_query' => array($tax_query)
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<aside class="grid-gallery four-col grid-gap-4">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$gallery_thumb = get_the_post_thumbnail_url( $page->ID, 'thumbnail' );
echo '<img src="'. $gallery_thumb .'" alt="" />';
}
echo '</aside>';
}
/* Restore original Post Data */
wp_reset_postdata();
}
add_shortcode( 'get_project_gallery', 'project_gallery_function' );
You shouldn't be worrying about the whole entire tax_query unless you plan on actually redoing the whole tax_query. You should consider changing it to just using $terms instead, since that's the only part you're replacing with the shortcode (you don't need to rebuild the whole array).
Secondly, make sure your code is normalized with your spacing and indentation - future you appreciates the forethought!
Lastly, you could consider passing even more variables to the array to make it a bit more extensible - $posts_per_page, $taxonomy etc.
function project_gallery_function( $atts, $content = null ){
extract( shortcode_atts( array(
'terms' => 'furniture-interior',
'field' => 'slug',
'order' => 'DESC',
'orderby' => 'date',
'taxonomy' => 'case_study_categories',
'post_type' => 'case_studies',
'posts_per_page' => 4,
), $atts ) );
$tax_query = array(
array(
'taxonomy' => $taxonomy,
'field' => $field,
'terms' => explode(',', preg_replace('/\s+/', '', $terms)), // Force to array, allowing comma sep values
)
);
$args = array(
'post_type' => $post_type,
'post_status' => 'publish',
'posts_per_page' => $posts_per_page,
'order' => $order,
'orderby' => $orderby,
'tax_query' => array( $tax_query )
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if( $the_query->have_posts() ){
echo '<aside class="grid-gallery four-col grid-gap-4">';
while( $the_query->have_posts() ){
$the_query->the_post();
if( $gallery_thumb = get_the_post_thumbnail_url( $page->ID, 'thumbnail' ) ){
printf( '<img src="%s" alt="" />', get_the_permalink(), $gallery_thumb );
}
}
echo '</aside>';
}
/* Restore original Post Data */
wp_reset_postdata();
}
add_shortcode( 'get_project_gallery', 'project_gallery_function' );
A few other notes, get_the_post_thumbnail_url() returns a falsey value if it doesn't exist, so you should wrap that in an if statement incase the image can't be found for some reason (corrupted, removed, forgot to add, etc). Also you have a $page->ID variable in get_the_post_thumbnail_url() - but don't seem to have a global $page in the function? Make sure that's the variable you want to use there.
Using code like what I've provided will let you do what you want, [get_project_gallery order="ASC" terms="flooring"], or even add multiple [get_project_gallery order="ASC" terms="flooring,something-else,a-third-thing"] - just match up the shortcode attributes with the parameter you want to override: [get_project_gallery order="ASC" terms="flooring,something-else,a-third-thing" posts_per_page="15"]

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

How to change Bulk Wordpress Post Status?

Is it possible to Published all Draft Posts using SQL or Function code? I tried using this code in my function file, but not works.
add_action('admin_init','wpse_244394');
function wpse_244394(){
$args = array('post_type'=> 'post',
'post_status' => 'draft',
'posts_per_page'=>-1
);
$draft_posts = get_posts($args);
foreach($draft_posts as $post_to_publish){
$query = array(
'ID' => $post_to_publish->ID,
'post_status' => 'publish',
);
wp_update_post( $query, true );
}
}
try to use "wp_publish_post( $post_id )" instead of "wp_update_post".
add_action('admin_init','wpse_244394');
function wpse_244394(){
$args = array('post_type'=> 'post',
'post_status' => 'draft',
'posts_per_page'=>-1
);
$draft_posts = get_posts($args);
foreach($draft_posts as $post_to_publish){
wp_publish_post( $post_to_publish->ID );
}
}

Wordpress foreach loop doesn't finish

I have a little under 1,000 posts of type lp_lesson and I am trying to be able to loop through them all to apply text to a specific custom field.
The below code works for only a few of them. Possibility between 10-15. I wanted to know if it's because I am using wp_enqueue_scripts? I also tried applying this change on wp_login. Both of them update the exact same number of posts and the exact same posts.
I am completely new to php so if I am doing this complete wrong please let me know.
I do believe there's an issue with the loop not finishing before the system just times it out, maybe? I can confirm that all the posts are lp_lesson so the loop is not finishing. Perhaps $args cannot hold an array that big? Any tips are appreciated!
Thanks.
add_action( 'wp_enqueue_scripts', 'win_9388244_format_lp_lesson' );
function win_9388244_format_lp_lesson() {
//Get post type of lp_lesson
$args = array(
'post_type' => 'lp_lesson'
);
$posts = get_posts($args);
foreach($posts as $post) {
update_post_meta( $post->ID, 'wpk_icon_text', 'Test' );
}
}
The default args of get_posts are :
$defaults = array(
'numberposts' => 5,
'category' => 0, 'orderby' => 'date',
'order' => 'DESC', 'include' => array(),
'exclude' => array(), 'meta_key' => '',
'meta_value' =>'', 'post_type' => 'post',
'suppress_filters' => true
);
meaning that if you do not specify these values in your $args variables, wordpress will take these ones by default. If you give a value to numberposts, then wordpress will use this one, and the other default values.
You should set the numberposts. For instance :
$args = array(
'post_type' => 'lp_lesson',
'numberposts' => 99999
);
get_posts function reference

search only in post titles in the wordpress to get titles and ids

I'm an android developer. I made a php file on my host and I include wp-blog-header.php . I used this file as a web service in the my application.
In the my app, there is a search part and I get term as String and category as id of user and send them to my php file .
now, I would like to search in post titles and return titles and ids of what user want.
function customSearch($term,$category){
.
. //To search by title and limit by category
.
}
I used prepare functions to get posts like below function, but I cannot find any function to search only in post titles.
function getLastItems()
{
$args = array(
'numberposts' => 5,
'offset' => 0,
'category' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true );
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
$mjson = array();
foreach( $recent_posts as $recent ){
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($recent['ID']), array(400,300) );
$url_img = $thumb['0'];
$marr = array(
'id'=>$recent["ID"],
'title'=>$recent["post_title"],
'img'=>$url_img
);
array_push($mjson,$marr);
}
return $mjson ;
}//end get last items

Categories