Category: WordPress

wp-admin redirect loop on https issue

Add below code in wp-config.php file

 

define(‘FORCE_SSL_ADMIN’, true);
define(‘FORCE_SSL_LOGIN’, true);
if ($_SERVER[‘HTTP_X_FORWARDED_PROTO’] == ‘https’)
$_SERVER[‘HTTPS’]=’on’;

Advertisements

How to check featured image exist or not in post

$args = array( ‘post_type’ => ‘post’, ‘posts_per_page’ => -1);

$the_query = new WP_Query( $args );
$i = 1;

if ( $the_query->have_posts() ) {

echo ‘<ul>’;

while ( $the_query->have_posts() ) {

$the_query->the_post();

if ( has_post_thumbnail() ) {

$dom_url = get_the_post_thumbnail_url();

$new_url = str_replace(‘site_url()’,’/var/www/html/’,get_the_post_thumbnail_url()); if(!file_exists($new_url))

{

echo ‘<li><b> ‘ .$i. ‘ </b> ‘. get_the_title() . ‘ Page Link:——-‘. get_the_post_thumbnail_url() .'</li>’;

$i++;

}

}

}

echo ‘</ul>’;

wp_reset_postdata();

} else {

// no posts found

}
echo “Number Of Posts: “.$i;

Check root path of image $_SERVER[‘DOCUMENT_ROOT’]

Remove All products or attributes with SQL commands WooCommerce

Remove all attributes from WooCommerce

DELETE FROM wp_terms WHERE term_id IN 
(SELECT term_id FROM wp_term_taxonomy WHERE taxonomy LIKE 'pa_%');
DELETE FROM wp_term_taxonomy WHERE taxonomy LIKE 'pa_%';
DELETE FROM wp_term_relationships WHERE term_taxonomy_id not IN 
(SELECT term_taxonomy_id FROM wp_term_taxonomy);

Delete all WooCommerce products

DELETE FROM wp_term_relationships WHERE object_id IN 
(SELECT ID FROM wp_posts WHERE post_type IN ('product','product_variation'));
DELETE FROM wp_postmeta WHERE post_id IN (SELECT ID FROM wp_posts WHERE post_type IN 
('product','product_variation'));
DELETE FROM wp_posts WHERE post_type IN ('product','product_variation');

Delete orphaned postmeta

DELETE pm
FROM wp_postmeta pm
LEFT JOIN wp_posts wp ON wp.ID = pm.post_id
WHERE wp.ID IS NULL

Add New Admin User in WordPress by MySql Query

INSERT INTO `wp_users` (`user_login`, `user_pass`, `user_nicename`, `user_email`, `user_status`)
VALUES (‘newadmin’, MD5(‘pass123’), ‘firstname lastname’, ‘email@example.com‘, ‘0’);

INSERT INTO `wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`)
VALUES (NULL, (Select max(id) FROM wp_users), ‘wp_capabilities’, ‘a:1:{s:13:”administrator”;s:1:”1″;}’);

INSERT INTO `wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`)
VALUES (NULL, (Select max(id) FROM wp_users), ‘wp_user_level’, ’10’);

How to list all Categories, Sub-Categories and Products in WooCommerce

<?php
$taxonomy = 'product_cat';
$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;

$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
?>

<?php
$all_categories = get_categories( $args );
foreach ($all_categories as $cat)
{

if($cat->category_parent == 0)
{
$category_id = $cat->term_id;
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
echo "<ul class='category'><li>".$cat->name;
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty

);

$sub_cats = get_categories( $args2 );
if($sub_cats)
{

foreach($sub_cats as $sub_category)
{
echo "<ul class='subcategory'>";
if($sub_cats->$sub_category == 0)
{
echo "<li>".$sub_category->cat_name;

?>
<?php
/*echo "<pre>";
print_r($sub_category);
echo "</pre>";*/

$args = array( 'post_type' => 'product','product_cat' => $sub_category->slug);
$loop = new WP_Query( $args );
echo "<ul class='products'>";
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?> <li>
<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php the_title(); ?>
</a></li>
<?php endwhile; ?>
</ul>
<?php wp_reset_query(); ?>

<?php
}
else
{
echo "</li></ul></li>";
}
echo "</ul>";
}
}
else
{
$args = array( 'post_type' => 'product', 'product_cat' => $cat->slug );
$loop = new WP_Query( $args );
echo "<ul class='products'>";
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?> <li>
<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php the_title(); ?>
</a></li>
<?php endwhile; ?>
</ul></li></ul>
<?php wp_reset_query();
}
}
else
{
echo "</li></ul>";
}
}
?>