Wordpress foreach loop doesn't finish - php

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

Related

WP_Query 'post__not_in' condition is not working: shows all posts

thank you for taking the time to review this.
I am calling a new WP_Query with the following arguments:
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'post__not_in' => array($featured_post_ID),
'category_name' => 'blog',
'offset' => $offset,
'posts_per_page' => 3,
'orderby' => 'post-date',
'order' => 'DESC',
);
I know that past issues have been caused by passing a numeric post ID as a variable instead of string; to get around that I am using the following variable definition for $featured_post_ID:
$featured_post_ID = '"'.$post_ID.'"';
When I run this query, it outputs the post I am trying to exclude.
Here is a Transcript of output of the WP_Query to save space. Looks like post__not_in is recognized in the query, but has no effect on the output.
Can anyone advise why this might be happening, or how to fix it?

Getting custom post type data including ACF with Timber?

I need to get custom post type data for usage with Timber and Twig, it's working fine using the standard WP_Query, such as:
// Get the testimonials custom post type posts
$args = array(
'post_type' => 'testimonial',
'post_status' => 'publish',
'perm' => 'readable',
'nopaging' => true
);
$context['testimonials'] = new WP_Query( $args );
// Restore the global $post to the current post in the main query
wp_reset_postdata();
However this obviously doesn't get ACF fields.
Was going to do the below as indicated in the Timber docs:
// Get the testimonials custom post type posts
$args = array(
'post_type' => 'testimonial',
'post_status' => 'publish',
'perm' => 'readable',
'nopaging' => true
);
$context['testimonials'] = Timber::get_posts( $args );
However, it seems that get_posts is getting deprecated in 2.0.
It seems like the best way to do it would be by using Timber's PostQuery, but because of lacking documentation I am unsure if it is more or less an encapsulation of WP_query and I can simply do something like:
// Get the testimonials custom post type posts
$args = array(
'post_type' => 'testimonial',
'post_status' => 'publish',
'perm' => 'readable',
'nopaging' => true
);
$context['testimonials'] = new PostQuery(array('query' => $args));
Am I on the right track here or what is the correct way?
Ok, after finding this answer and also digging through the code a bit I found out how to do it I believe:
// Get the testimonials custom post type posts
$args = array(
'post_type' => 'testimonial',
'post_status' => 'publish',
'perm' => 'readable',
'nopaging' => true
);
$query = new Timber\PostQuery($args);
// Get an array of post objects
$context['posts'] = $query->get_posts();
From what I can tell, all the respective WP_Query args seems to work using this method as well - but if you need pagination to work I would advise reading that other answer I linked to as it contains more information on that.
ACF fields have some weird numerical keys in the database. To get the data just use their own function
get_fields();
https://www.advancedcustomfields.com/resources/get_fields/

WP_Query: How to test for sticky posts before assigning posts_per_page?

Is there any way to 'test' whether there are Sticky Posts?
If there IS a sticky post, I'd like to use 'posts_per_page' => '3' and if not, use 4, as I'm finding that Sticky Posts add to the 'posts per page' which breaks the layout :(
I have a WP_query as follows:
$article_args = array(
'post_type' => 'post',
'posts_per_page' => '3',
'post_status' => 'publish',
'order' => 'DESC'
);
$article_query = new WP_Query($article_args);
Is that possible, maybe to test for this before the args array and use the appropriate number?
Yes, I have considered ignore_sticky_posts but I do want to include them still.
Any advice from someone who knows PHP better than me?! :/
Thanks. Martin.
A sticky post is saved as an option which you can check with $sticky = get_option( 'sticky_posts' );. This returns an array with post ID's. You can simply just check if the option is empty or not, and then modify your arguments accordingly
You can do something like this
$sticky = get_option( 'sticky_posts' );
if( $sticky ) {
$article_args = array(
'post_type' => 'post',
'posts_per_page' => '3',
'post_status' => 'publish',
'order' => 'DESC'
);
}else{
$article_args = array(
'post_type' => 'post',
'posts_per_page' => '4',
'post_status' => 'publish',
'order' => 'DESC'
);
}
$article_query = new WP_Query($article_args);
Unfortunately not, but there probably is another way to achieve what you're after.
You could simply grab the latest sticky post, and show that first, and then, in a second query, grab the rest of the posts (or the next 3, anyway).
That way, you'll get 1 sticky (if it exists) and 3 normal, per page.
We don't know your end goal here, so this could be unusable for you.

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' ];

WP Loop with custom array field as argument

I'm trying to do a query with the arguments below. Somehow, WP just doesn't return any posts. I can't figure out what I'm doing wrong! Any help?
Some more info: I have a custom post type that contain featured images. I want them to be displayed in a header slider. Through Advanced Custom Fields plugin I've created a custom field in the posts: 'assigned_page'. It's an array with page ID's on which that specific slide should be displayed. '$current_page' is the ID of the current page that's to be displayed. So, $args should filter the custom post type, and the posts that have the current page ID in their 'assigned_page' array.
// Get the current page ID
global $post;
$current_page = $post->ID;
$string_page = (string)$current_page;
$current_parent_page = $mv_is_subpage->ID;
// Post selection
$args = array (
'post_type' => $post_type,
'posts_per_page' => $posts_per_page,
'orderby' => $orderby,
'order' => $order,
'no_found_rows' => 1,
'meta_query' => array(
array(
'meta_key' => 'assigned_page',
'meta_value' => $string_page,
)
),
);
Then:
$query = new WP_Query( $args );
And then the loop:
while ($query->have_posts()) : $query->the_post();
I guess it should be like this (according to docs at http://codex.wordpress.org/Class_Reference/WP_Query) :
array(
'key' => 'assigned_page',
'value' => $string_page,
)

Categories