After a long search on the net, my code didn't come up with what I wanted.
I with wordpress and acf , the purpose of the function is to return a mapped array from a costumed post type.
I created and saved the costum post type named 'myCpt' to which I linked it with acf type fields: (text and repeater)
I was able to retrieve the text value but I can't get the repeater values.... Thanks for seeing the var_dump() image
Your help will be very valuable to me.
class myClass
{
function __construct()
{
var_dump($this->get_all());
}
function get_all()
{
$args = array(
'post_type' => 'myCpt',
'posts_per_page' => 999,
'post_status' => 'publish'
);
$myCpt = get_posts($args);
$myCpt_mapped = array_map(function ($myCpt) {
$id = $myCpt->ID;
return array(
'id' => $id,
'title' => get_field('maintitle', $id, false),
'subtitle' => get_field('subtitle', $id, false),
// 'services' => services repeater
'repField' => array(
'name' => get_sub_field('name', $id, false),
'img' => get_sub_field('img', $id, false)
)
);
}, $myCpt);
return array(
'myCpt' => $myCpt_mapped
);
}
}
global $myClass;
$myClass = new $myClass();}
resultat:
var_dump()
Related
I have a custom hook to show custom post types in a select field on a form. I am trying to pull specific categories from those custom post types only. Currently I have it working but it pulls all the posts from the selected custom post types into the select field. When I add any Category Parameters or Tag Parameters it stops working.
Here is my code:
add_filter('frm_setup_new_fields_vars', 'add_rental_field_function', 3, 21);
add_filter('frm_setup_edit_fields_vars', 'add_rental_field_function', 3, 21);
function add_rental_field_function($values, $field){
if($field->id == 21){
$arrayPositions = array();
$pushobj = array(
'value' => ""
);
array_push($arrayPositions, $pushobj);
$positionQuery = new WP_Query( array(
'post_type' => array ('available_rentals', 'farrell_storage', 'farrell_commercial'),
'posts_per_page' => -1,
'orderby' => "menu_order",
'order' => "ASC",
) );
if ( $positionQuery->have_posts() ) {
while ( $positionQuery->have_posts() ) {
$positionQuery->the_post();
$title=get_the_title();
$pushobj = array(
'value' => get_the_title(),
'label' => $label
);
array_push($arrayPositions, $pushobj);
}
wp_reset_postdata();
}
$values['options'] = $arrayPositions;
}
return $values;
}
In my WordPress v5.5.1, I am using public front-end form to create custom post type. I am saving the form data as custom post with below code:
function save_function() {
// MESSAGE FIELDS
$public_post = array(
'post_title' => filter_input(INPUT_POST, 'title'),
'post_author' => 1,
'post_type' => 'message',
'post_status' => 'pending',
'tax_input' => array(
'my_custom_taxonomy' => array(filter_input(INPUT_POST, 'subject')) // subject is a HTML select which captures option value contains term_id which is generated using get_terms.
)
);
wp_insert_post($public_post);
}
Instead of 'tax_input' tag this post to the existing custom_taxonomy term, it is creating a duplicate term with term_id as term name.
Hello Plese Try This Func
function save_function()
{
$subject_term = 'subject';
$my_subject_term = term_exists($subject_term, 'my_custom_taxonomy'); // check if term in website or no
// Create Term if it doesn't exist
if (!$my_subject_term) {
$my_subject_term = wp_insert_term($subject_term, 'my_custom_taxonomy');
}
$custom_tax = array(
'my_custom_taxonomy' => array(
$my_subject_term['term_taxonomy_id'],
)
);
// MESSAGE FIELDS
$public_post = array(
'post_title' => filter_input(INPUT_POST, 'title'),
'post_author' => 1,
'post_type' => 'message',
'post_status' => 'pending',
'tax_input' => $custom_tax
);
$post_id = wp_insert_post($public_post);
}
I am using the Thrive visual editor, for the "Post grid" element, when you go inside the grid options, there is a field there that says "Individual Posts / Pages" which it only pulls post/pages and my custom post type is not being pulled
Here is the code found inside "class-tcb-postgrid-element.php"
class TCB_Postgrid_Element extends TCB_Element_Abstract {
/**
* Constructs the post lists for "Individual Post / Pages filter"
*
* #return array
*/
public static function get_posts_list( $term ) {
$args = array(
'order_by' => 'post_title',
'post_type' => array( 'page', 'post' ),
'post_status' => array( 'publish' ),
's' => $term,
);
$results = new WP_Query( $args );
$list = array();
foreach ( $results->get_posts() as $post ) {
$list[] = array( 'id' => $post->ID, 'text' => $post->post_title );
}
return $list;
}
//... other functions here
}
as you can see if I add a post type it works but I am editing the plugin file itself
'post_type' => array( 'page', 'post', 'application', 'services' ),
How can I modify this so that I can add it in my functions.php file?
Well am creating my first wordpress plugin, which should create a page (relatively large) on activation.
Currently i can create a file using :
$_p['post_title'] = $the_page_title;
$_p['post_content'] = '<h1>PAGE CONTENT</h1>';
$_p['post_status'] = 'publish';
$_p['post_type'] = 'page';
$_p['comment_status'] = 'closed';
$_p['ping_status'] = 'closed';
$_p['post_category'] = array(1); // the default 'Uncatrgorised'
// Insert the post into the database
$the_page_id = wp_insert_post( $_p );
The problem is, I cant put all the content of the file(which is large) as string to 'post_content' index. I want to know if there is a way, where i can simply either:
'post_content' => link to the file in my plugin directory
'post_content' => call a function which will return html content as string :( [worst case]
OR, Some more simpler way to achieve the objective.
Please help me.
Well, what i did to solve the problem is:
//**SECTION : 1**
function user_login_foo() {
return get_login_form(); // get_login_form() function will return the html template i want to display.
}
add_shortcode('user_login', 'user_login_foo'); // created a shortcode
**// SECTION: 2**
function get_login_form()
{
ob_start(); ?>
<h3><?php __('Login'); ?></h3>
<form action="" method="post">
<fieldset>
// the login form comes here
</fieldset>
</form>
<?php
return ob_get_clean();
}
function validate_login_user() {
// the login validation logic comes here
}
add_action('init', 'validate_login_user');
SECTION 1: registered a shortcode that will call a function [say,foo1()] and return the value.
The function foo1() calls another a function [say foo2()] which returns a clean html form in response to the call (when the shortcode is called).
SECTION 2: In this section I defined the function foo2(), within which the html form [login form] is defined and returned to foo1() [where it is displayed].
Then i created an action [ add_action('init', 'validate_login_user'); ] which will call the function validate_login_user() on initialization, inside this function i checked for isset(METHOD[username]) and isset(METHOD[password]) and then do respective logic.
Like this i created multiple [shortcodes] for each of the pages I wanted to create at the time of activation,
Then :
**step 1:** register_activation_hook(__FILE__,'activation_plugin');
**step 2:** activation_plugin(){
'390' => [ // '390' is page id
'post_title' => 'Page title say login',
'post_content' => "[user_login]",
'post_status' => 'publish',
'post_type' => 'page',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_category' => array(1)
],
'391' => [
'post_title' => 'page title 2',
'post_content' => "[short_code2]",
'post_status' => 'publish',
'post_type' => 'page',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_category' => array(1)
],
// like this add multiple shortcodes
}
// this foreach will create all the pages
foreach ($_ as $key => $value) {
$the_page_title = $value['post_title'];
$the_page_name = $value['post_title'];
// the menu entry...
delete_option($value['post_title']);
add_option($value['post_title'], $the_page_title, '', 'yes');
// the slug...
delete_option($value['post_title']);
add_option($value['post_title'], $the_page_name, '', 'yes');
// the id...
delete_option($key);
add_option($key, '0', '', 'yes');
$the_page = get_page_by_title( $the_page_title );
if ( ! $the_page ) {
$the_page_id = wp_insert_post( $value );
}
else {
$the_page_id = $the_page->ID;
$the_page->post_status = 'publish';
$the_page_id = wp_update_post( $the_page );
}
delete_option( $key );
add_option( $key, $the_page_id );
}
Create Page On Plugin activation with content
$page_content= 'Content of page';
$demo_page = array(
'comment_status' => 'closed',
'ping_status' => 'closed' ,
'post_author' => 1,
'post_content' => $page_content,
'post_date' => date('Y-m-d H:i:s'),
'post_name' => 'page_name',//display on address bar
'post_status' => 'publish' ,
'post_title' => 'Page Display Name',
'post_type' => 'page',
);
//insert page and save the id
$demo_page_value = wp_insert_post( $demo_page, false );
//save the id in the database
update_option( 'testpage', $$demo_page_value );
You Use and Guide and read this link How to Create plugin and pages https://codex.wordpress.org/Creating_Options_Pages
I'm using Visual Composer in WordPress and I want to make a custom Post Grid. But the default elements that the post grid supplies are not enough. I want to show the author of the post, the number of comments it has, the category it has and the Tags it has as well. I'm not really familiar with Visual Composer, but I need a point in the right direction for me to get this data? What can I do? I've search their documents but with no luck. If I need to move around the php code I would like to know what I'm moving around is the right thing. Any ideas? If you need any more information please do ask :D
Thanks in advance for the help.
If anybody is still looking to find out how to to get the id in a post grid or create a specific widget for the grid you can use the following snippet I creatd for adding an icon before the post title. You will see inside the first function you can call $post-ID for use on any query.
//** Case Study Title Block Shortcodes ***********
//********************************
add_filter( 'vc_gitem_template_attribute_case_study_title','vc_gitem_template_attribute_case_study_title', 10, 2 );
function vc_gitem_template_attribute_case_study_title( $value, $data ) {
extract( array_merge( array(
'post' => null,
'data' => '',
), $data ) );
$atts_extended = array();
parse_str( $data, $atts_extended );
$atts = $atts_extended['atts'];
// write all your widget code in here using queries etc
$title = get_the_title($post->ID);
$link = get_permalink($post->ID);
$terms = get_the_terms($post->ID, 'case_categories');
$output = "<h4 class=\"case-title\">". get_the_icon($terms[0]->term_id) . $title ."</h4>";
return $output;
}
add_filter( 'vc_grid_item_shortcodes', 'case_study_title_shortcodes' );
function case_study_title_shortcodes( $shortcodes ) {
$shortcodes['vc_case_study_title'] = array(
'name' => __( 'Case Study Title', 'sage' ),
'base' => 'vc_case_study_title',
'icon' => get_template_directory_uri() . '/assets/images/icon.svg',
'category' => __( 'Content', 'sage' ),
'description' => __( 'Displays the case study title with correct icon', 'sage' ),
'post_type' => Vc_Grid_Item_Editor::postType()
);
return $shortcodes;
}
add_shortcode( 'vc_case_study_title', 'vc_case_study_title_render' );
function vc_case_study_title_render($atts){
$atts = vc_map_get_attributes( 'vc_case_study_title', $atts );
return '{{ case_study_title }}';
}
I got the same issue; here is how I solve it:
According to Visual Composer documentation: https://kb.wpbakery.com/docs/developers-how-tos/adding-custom-shortcode-to-grid-builder/
When you add the code below to functions.php, a new component will be added in your custom grid builder (in my example the name will be "Author"). There are a number of values you can get by post data template variable function and one of it not the name of the author but their ID (that's sad but at least you can use this value to get the author name).
The value is 'post_author' => ID of the author (for example '1')
Here is the function where I get the post author and display it (if author component was added to your custom grid in "custom grid builder"). Put it in functions.php of your child theme:
add_filter( 'vc_grid_item_shortcodes', 'my_module_add_grid_shortcodes' );
function my_module_add_grid_shortcodes( $shortcodes ) {
$shortcodes['vc_post_id'] = array(
'name' => __( 'Author', 'my-text-domain' ),
'base' => 'vc_post_id',
'category' => __( 'Content', 'my-text-domain' ),
'description' => __( 'Show current post author', 'my-text-domain' ),
'post_type' => Vc_Grid_Item_Editor::postType(),
);
return $shortcodes;
}
// output function
add_shortcode( 'vc_post_id', 'vc_post_id_render' );
function vc_post_id_render() {
$nn = '{{ post_data:post_author }}'; // usage of template variable post_data with argument "post_author"
return get_the_author($nn);
}
There is a little problem. It works but get_the_author is a deprecated function in WordPress. I'd appreciate any suggestions to make it more modern or if you name other alternatives please suggest.
Also, here is the list of available variables of vc_post_id_render from docs. Here they are:
WP_Post::__set_state(array(
'ID' => 69,
'post_author' => '1',
'post_date' => '2015-04-29 14:15:56',
'post_date_gmt' => '2015-04-29 14:15:56',
'post_content' => 'Your post content',
'post_title' => 'Your post title',
'post_excerpt' => '',
'post_status' => 'publish',
'comment_status' => 'open',
'ping_status' => 'open',
'post_password' => '',
'post_name' => 'post name',
'to_ping' => '',
'pinged' => '',
'post_modified' => '2015-06-17 11:18:41',
'post_modified_gmt' => '2015-06-17 11:18:41',
'post_content_filtered' => '',
'post_parent' => 0,
'guid' => 'http://wp.master/?p=69',
'menu_order' => 0,
'post_type' => 'post',
'post_mime_type' => '',
'comment_count' => '0',
'filter' => 'raw',
'filter_terms' =>
array (
),
))
this is your response
https://kb.wpbakery.com/docs/developers-how-tos/adding-custom-shortcode-to-grid-builder/
at Template variables usage
Exemple, in visual composer template you can use {{ post_date:ID }} to show post ID. I don't know how show tag.