In my phpAdmin I have a list of arrays that I need to be able to access in php Wordpress.
I need to get these two arrays and the variables associated with them but I can't find anywhere how to access this type of information.
Essentially I would like to loop through all these items and match one of their variables with the ID of specific posts - I have the post part.
wp_learnpress_sections
wp_learnpress_section_items
Basically the item_type is an lp_lesson which is a custom post type. I am able to grab all the posts from wp_posts so I figured I would be able to grab these other arrays?
Edit:
My theme function. This works for all the posts. However, I want to be able to find out which section_id a post belongs to.
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',
'numberposts' => 99999
);
$posts = get_posts($args);
foreach($posts as $post) {
update_post_meta( $post->ID, 'wpk_icon_text', 'Test' );
}
}
Full function that does it:
function win_9388244_format_lp_lesson() {
//Get post type of lp_lesson
$args = array(
'post_type' => 'lp_lesson',
'numberposts' => 99999
);
$posts = get_posts($args);
global $wpdb;
$sections = $wpdb->get_results( "SELECT section_item_id, section_id, item_id FROM wp_learnpress_section_items", ARRAY_A );
$items = $wpdb->get_results( "SELECT section_course_id, section_id FROM wp_learnpress_sections", ARRAY_A );
foreach($posts as $post) {
$lesson_id = $post->ID;
foreach($sections as $section) {
if($section['item_id'] == $lesson_id) {
$currentSection = $section['section_id'];
foreach($items as $item) {
if ($item['section_id'] == $currentSection) {
$course = $item['section_course_id'];
//switch $course with predefined variables for courses
}
}
}
}
}
}
Related
I build a form that allow users to register sells using the frontend. I'm trying to make it pass some taxonomies that will be used to create a filter later, but even with wp_set_post_terms function the taxonomy is not being checked/tick'd at the backend when the user add/select it at the frontend.
Here is the code snippet:
<?php
global $wpdb;
global $wp_taxonomies;
$user_id = get_current_user_id();
if(isset($_POST['submitted'])) {
if($_POST['submitted'] == 'f16fca78e22e3f296692e5bbe0b5f7f0597a0bf9') {
$codigo_reserva = $_POST['codigo-reserva'];
$pax_principal = $_POST['pax-principal'];
$tag_acomodacao = $_POST['tag-acomodacao'];
$quantidade_pax = $_POST['quantidade-pax'];
$check_in = $_POST['check-in'];
$check_out = $_POST['check-out'];
$valor_venda = $_POST['valor-venda'];
$args = array(
'post_type' => 'vendas',
'post_title' => $codigo_reserva,
'post_status' => 'private',
'post_author' => $user_id,
);
$post_id = wp_insert_post($args);
$post_tags = array($tag_acomodacao);
wp_set_post_terms($post_id, $post_tags, 'acomodacao_reserva', true);
update_field('venda_pax_principal', $pax_principal, $post_id);
update_field('venda_pax_qtd', $quantidade_pax, $post_id);
update_field('checkin_venda', $check_in, $post_id);
update_field('checkout_venda', $check_out, $post_id);
update_field('venda_valor', $valor_venda, $post_id);
}
}
Everything works well except the taxonomy
Already tried to initialize $wp_taxonomies, use the explode function at the second parameter of wp_set_post_terms, pass the array as slug, and pass the array as ID
I making custom endpoint for my custom post type. The one thing is the taxonomy ID. I need to get taxonomy ID by post ID. get_terms( 'gallery_tax', $post->ID ) gives me array with all taxonomy object
function wl_posts() {
$args = [
'numberposts' => 9999,
'post_type' => 'gallery',
];
$posts = get_posts($args);
$data = [];
$i = 0;
foreach ($posts as $post) {
$data[$i]['id'] = $post->ID;
$data[$i]['fimg_url'] = get_the_post_thumbnail_url($post->ID, 'large');
$data[$i]['proizvoditel'] = get_field('proizvoditel', $post->ID);
$data[$i]['tip'] = get_field('tip', $post->ID);
$data[$i]['razmer'] = get_field('razmer', $post->ID);
$data[$i]['forma'] = get_field('forma', $post->ID);
$data[$i]['rost'] = get_field('rost', $post->ID);
$data[$i]['ves'] = get_field('ves', $post->ID);
$data[$i]['ohvat'] = get_field('ohvat', $post->ID);
$data[$i]['vozrast'] = get_field('vozrast', $post->ID);
$data[$i]['galereya'] = get_field('galereya', $post->ID);
$data[$i]['taxonomy'] = get_terms( 'gallery_tax', $post->ID );
$i++;
}
return $data;
}
add_action('rest_api_init', function() {
register_rest_route('wl/v1', 'gallery', [
'methods' => 'GET',
'callback' => 'wl_posts',
]);
});
Using get_terms('gallery_tax') will give you all the terms in a taxonomy.
https://developer.wordpress.org/reference/functions/get_terms
You get all the existing terms in your taxonomy. So this is why you get the result.
Using get_the_terms($post->ID, 'gallery_tax') will give you all taxonomy terms attached to the post.
https://developer.wordpress.org/reference/functions/get_the_terms/
You get all the terms that have been assigned to your post.
If you want to display the name of the taxonomy itself and not display the terms associated with the post, you can first get all the names of the taxonomies outside of your post loop and then get the taxonomy name inside of your foreach:
...
$data = [];
$i = 0;
$taxnames = get_taxonomies('','names');
foreach ($posts as $post) {
...
$data[$i]['taxonomy'] = wp_get_post_terms($post->ID, $taxnames, array("fields" => "names"));
$i++;
}
...
https://developer.wordpress.org/reference/functions/get_taxonomies/
I have a solution for the fact that users can now add categories to their posts. Problem: they don't know which already exist and which do. Therefore, I'd like to go to a route where users can choose (checkbox?) the categories that exist.
My question: how to do this properly?
My code is as follows:
if(isset($_POST['entry']) AND !$_POST['entry'] == ""):
$my_post = array();
$my_post['post_title'] = $_POST['title'];
$my_post['post_content'] = $_POST['entry'];
$my_post['post_status'] = 'publish';
$cat_name = sanitize_text_field( $_POST['newcat'] );
$my_post ['cat_name'] = $cat_name;
$category_id = get_cat_ID( $_POST['newcat'] );
if ( ! $category_id ) {
if ( ! is_admin() ) {
die();
}
$args = array(
'description' => "Category description",
'parent' => 0);
$category_id = wp_insert_term( $_POST['newcat'], "category", $args );
}
$my_post['post_author'] = get_current_user_id();
$my_post['tax_input'] = array('category' => $category_id);
wp_insert_post( $my_post );
And then I am showing the dropdown categories, yet I can't save my choice when adding the checkboxes for categories.
$categories=get_categories(); foreach($categories as $category) { echo "<input type='checkbox' name='mychecky' value='$category->term_id' />"; echo $category->cat_name;
echo '<br>'; }
How can I save the chosen category per checklist for my post?
In your form checklist should accept multiple values so it must be an array. Array in HTML forms has square brackets [] so your checkbox name should look like mychecky[]. The full code for checkbox inputs:
$categories = get_categories();
foreach($categories as $category) {
echo "<label><input type='checkbox' name='mychecky[]' value='$category->term_id' />$category->cat_name</label><br>";
}
Then when you check POST data you should expect an array from the form and you can assign it like it is since post_category parameter must be an array anyway:
// it is an array from a form with category IDs
if (isset($_REQUEST['mychecky'])) {
$my_post['post_category'] = $_REQUEST['mychecky'];
}
You can use your methods with taxonomy or use built in post_category, check docs for wp_inset_post function.
What i want to make:
I want to make a navigation menu where every dropdownbar is its own post type.
Movie
movie1
movie2
Book
book1
book2
Game
game1
game2
What i've made so far:
I'm not the best at php yet, but i tried to work something out:
echo "<ul class="menu">";
$post_type = get_post_types( array('Movie', 'Book', 'Game') );
foreach( $post_type as $type ) {
$args = array(
'post_type' => $type
);
echo "<li>".$type."<ul class="dropdown">";
$posts = get_posts( $args );
if( $posts ) {
foreach( $posts as $post ) {
echo "<li>".get_the_title( $post->ID, 'title' )."</li>";
}
echo "</ul></li>";
}
}
echo "</ul>";
Question:
Is there a smarter way to make the dropdownmenu? or what can i do to make it work?
There's nothing wrong with that approach, except that you shouldn't use get_post_types() - just an array of the post type names will do.
As it stands at the moment, 'post_type' => $type will pass an array to post_type, when it should be a string.
Also, echo "<li>".$type."<ul class="dropdown">"; should be inside your if( $posts ) { before the foreach.
Below is my working code to change a custom text field in every post of type lp_lesson to some text. Each one of these lp_lessons are assigned a specific post (lp_course).
My question is how do I figure out which lesson is connected to which course? I have a space in my phpAdmin title wp_learnpress_section_items that have a section_id and an item_id but I don't know how to access these as I have tried: $field_value = get_post_meta( $post->ID, 'section_id', 1);
The section_id references an array in my phpAdmin of wp_learnpress_sections and I would love to do this:
get the item_id which is equivalent to the post's ID. Then get the section_id associated with that item_id. Then get the section_course_id associated with that section_id. I have the logic down - I just don't know how to access these variables or items. I am guessing my $field_value is not the correct way to access these items.
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',
'numberposts' => 99999
);
$posts = get_posts($args);
foreach($posts as $post) {
//if category == Test
update_post_meta( $post->ID, 'wpk_icon_text', 'Test' );
}
}
Edit: I have found that $post->ID gives me the post's ID, but not the rest of the variables.
For anyone who wants to do something similar I got it to work.
function win_9388244_format_lp_lesson() {
//Get post type of lp_lesson
$args = array(
'post_type' => 'lp_lesson',
'numberposts' => 99999
);
$posts = get_posts($args);
global $wpdb;
$sections = $wpdb->get_results( "SELECT section_item_id, section_id, item_id FROM wp_learnpress_section_items", ARRAY_A );
$items = $wpdb->get_results( "SELECT section_course_id, section_id FROM wp_learnpress_sections", ARRAY_A );
foreach($posts as $post) {
$lesson_id = $post->ID;
foreach($sections as $section) {
if($section['item_id'] == $lesson_id) {
$currentSection = $section['section_id'];
foreach($items as $item) {
if ($item['section_id'] == $currentSection) {
$course_code = $item['section_course_id'];
if ($course_code == 6177){
update_post_meta( $post->ID, 'wpk_icon_text', 'Time & Labor' );
}
else{
update_post_meta( $post->ID, 'wpk_icon_text', 'Lesson' );
}
}
}
}
}
}
}