Reputation: 907
In Wordpress I am using the LearnPress plugin. This plugin creates custom posts (to my knowledge) called lp_lesson
. I am also using a plugin called Advanced Custom Fields PRO. This plugin allows the creation of additional fields on each of the lp_lesson
. The custom field I added has a name (which I am guessing is the variable name) of wpk_role_options
.
What I would like to do is come up with a for-loop
that loops through each lp_lesson
and checks the category
associated with that lessons using a switch
or if else
. If the condition is met I would like to add text to the wpk_role_options
custom field.
I think I have the logic complete for the most part, but it's more of the syntax that has me stumped.
Any help would be greatly appreciated!
What I have gathered so far:
<?php
//Get post type of lp_lesson
$args = array(
'post_type' => 'lp_lesson'
);
$post_query = new WP_Query($args);
//Categories of lp_lesson
$categories = get_categories($args);
if($post_query->have_posts() ) {
while($post_query->have_posts() ) {
foreach($categories as $category) {
if (in_category('Test', $post_query)){
//change wpk_role_options from lesson
}
}
}
}
?>
Upvotes: 1
Views: 143
Reputation: 11210
You are using the "WordPress loop" syntax which is used to display post lists on the public side. However, you wouldn't really want to use that on the admin side.
You need to use a normal php loop. You would also probably want to use the faster and easier get_posts
function:
//Get post type of lp_lesson
$args = array(
'post_type' => 'lp_lesson'
);
$posts = get_posts($args);
$categories = get_categories();
for($posts as $post) {
foreach($categories as $cat) {
if ( in_category( $cat->term_id, $post) ){
// This post has the category
}
}
// get/check for a post custom field
$field_value = get_post_meta( $post->ID, 'meta_name', 1);
// or you can also update this post's custom field
update_post_meta( $post->ID, 'meta_name', 'new_value' );
}
}
I don't really know why you are looping through the categories. If you are only checking for a specific category, you can take out the whole foreach around the $categories thing.
Upvotes: 2