Skip to content

Instantly share code, notes, and snippets.

View someguy9's full-sized avatar
🏠
Working from home

Andy Feliciotti someguy9

🏠
Working from home
View GitHub Profile
<?php
// Update a post programmatically
$my_post_id = 15
$update_post = array(
'ID' => $my_post_id,
'post_title' => 'My new post title',
'post_content' => 'Overwrite post content',
'post_status' => 'public'
);
@someguy9
someguy9 / wordpress-insert-custom-post.php
Last active February 14, 2023 17:46
Insert a custom post type programmatically in WordPress https://smartwp.com/wordpress-insert-post-programmatically/
<?php
// Insert custom post programmatically
$new_post = array(
'post_title' => 'My new example post',
'post_content' => 'My new content!',
'post_status' => 'public',
'post_type' => 'my_post_type'
);
$post_id = wp_insert_post( $new_post );
<?php
// Insert post programmatically
$new_post = array(
'post_title' => 'My new post',
'post_content' => 'Content to insert.',
'post_status' => 'publish'
);
$post_id = wp_insert_post( $new_post );
<?php
// Overwrite MightyShare title font-weight
function mightyshare_set_font_weight( $template_options ) {
$template_options['title_fontWeight'] = '900';
return $template_options;
}
add_filter( 'mightyshare_filter_post', 'mightyshare_set_font_weight', 10, 3 );
<?php
// Overwrite MightyShare fallback image
function mightyshare_overwrite_fallback_image( $template_options ) {
var_dump(get_post_meta( $template_options['ID'], 'mightyshare_enabled', true ));
if ( get_post_thumbnail_id($template_options['ID']) !== $template_options['background'] && empty( get_post_meta( $template_options['ID'], 'mightyshare_background', true ) ) ) {
$template_options['background'] = 'https://images.unsplash.com/photo-1674757273875-e47dbfbda82c?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2831&q=80';
}
return $template_options;
}
<body style="margin:0;padding:0;height:100vh;width:100vw;">
<div style="display:flex;align-items:center;height:100%;width:100%;justify-content:center;background:url('{{background}}')">
<h1 style="font-size:3.2em;color:#fff;text-shadow:0 8px 8px rgba(0,0,0,0.3)">{{title}}</h1>
</div>
</body>
<?php
// Get the current post or page's slug
$slug = get_post_field( 'post_name', get_post() );
echo $slug;
// Get slug for a post or page by ID
$post_id = 1;
echo get_post_field( 'post_name', $post_id );
@someguy9
someguy9 / wordpress-post-slug.php
Created October 31, 2022 16:23
Get the current post slug in WordPress
<?php
global $post;
$slug = $post->post_name;
<?php
$meta_query_args = array(
'meta_query' => array(
array(
'key' => 'my_date_field',
'value' => array( strtotime('2022-01-01'), strtotime('2022-12-31') ),
'type' => 'numeric',
'compare' => 'BETWEEN'
)
)