Export list of pretty permalinks and post title - mysql

Looking for a way to export a list of pretty permalinks in WordPress with the corresponding post title. Looking for the actual permalink structure defined not the shortlink. I suppose if I have to, I will use a short link, but I prefer the full permalink.

Here's a standalone PHP file you can save into the root of your website called something like /export.php and when you call it with your browser it will send a tab-delimited plain text list of posts with the pretty permalink, the post title and (as a bonus) the post type.
Just load the URL in your browser and then "save as" to a text file you can then load in Excel or however else you need to process it.
<?php
include "wp-load.php";
$posts = new WP_Query('post_type=any&posts_per_page=-1&post_status=publish');
$posts = $posts->posts;
/*
global $wpdb;
$posts = $wpdb->get_results("
SELECT ID,post_type,post_title
FROM {$wpdb->posts}
WHERE post_status<>'auto-draft' AND post_type NOT IN ('revision','nav_menu_item')
");
*/
header('Content-type:text/plain');
foreach($posts as $post) {
switch ($post->post_type) {
case 'revision':
case 'nav_menu_item':
break;
case 'page':
$permalink = get_page_link($post->ID);
break;
case 'post':
$permalink = get_permalink($post->ID);
break;
case 'attachment':
$permalink = get_attachment_link($post->ID);
break;
default:
$permalink = get_post_permalink($post->ID);
break;
}
echo "\n{$post->post_type}\t{$permalink}\t{$post->post_title}";
}
Hope this helps.
-Mike
P.S. I used the standard WordPress WP_Query() but also included a commented-out SQL in case you prefer (or need) to use it instead.

answered this one on EE this morning :)
http://wp.daveheavyindustries.com/2011/02/08/wordpress-permalink-via-sql/
this query should do it for you
SELECT wpp.post_title,
wpp.guid,
wpp.post_date,
CONCAT
(
wpo_su.option_value,
REPLACE
(
REPLACE
(
REPLACE
(
REPLACE
(
wpo.option_value,
'%year%',
date_format(wpp.post_date,'%Y')
),
'%monthnum%',
date_format(wpp.post_date, '%m')
),
'%day%',
date_format(wpp.post_date, '%d')
),
'%postname%',
wpp.post_name
)
) AS permalink
FROM wp_posts wpp
JOIN wp_options wpo
ON wpo.option_name = 'permalink_structure'
AND wpo.blog_id = 0
JOIN wp_options wpo_su
ON wpo_su.option_name = 'siteurl'
AND wpo_su.blog_id = wpo.blog_id
WHERE wpp.post_type = 'post'
AND wpp.post_status = 'publish'
ORDER BY wpp.post_date DESC

I also wanted this solution and thanks #MikeSchinkle for the original solution. I did use this to export those links in plain text to excel and then build my redirect list.
But then I found that I also wanted a solution with live, active links.
So I used the wp_query using the post type "any" and created a page template with a search form included with the following query (customize to fit your theme as you see fit). Note I had to set the posts_per_page at -1 to return unlimited results. This returns results as: "Title - Permalink"
<?php
$type = 'any';
$args = array (
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => -1,
'order' => 'DESC',
);
$temp = $wp_query; // assign ordinal query to temp variable for later use
$wp_query = null;
$wp_query = new WP_Query($args);
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post();
?>
<?php the_title(); ?> - <?php the_permalink() ?><br />
<?php endwhile; ?>
<?php else :
echo '<h2>Sorry, we didnt find any results to match. Please search again below or call us at 800-828-4228 and we will be happy to help!</h2>';
get_search_form();
endif;
$wp_query = null;
$wp_query = $temp; // Reset
?>
Hope that helps others.

Related

ACF front end form to update term

I want to use ACF frontend form function to create a form with custom fields
I see this issue for create new term, #Alhana
ACF front end form to create term
but I want to generate the form with old data
Well, i didn't see that question, but if it's still actual, here's a solution.
First of all, make sure you have ACF group, linked to your taxonomy. You will need ID of this group, it can be found in url on group edit page, for example:
http://site.ru/wp-admin/post.php?post=340&action=edit
In this case group ID is 340. If you don't want to use hardcoded ID (if your groups are changing from time to time), you can get it, using group name (in this example group name is Technic CPT):
global $wpdb;
$group_ID = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_title = 'Technic CPT'" );
Then, you'll need ID of term you're updating. I think, it's not nesessary to write about getting it since it's WP basics :) You'll end with something like this:
$term_id = 405;
And finally, you'll need your taxonomy's slug. In this example it's technic. So, let's render our form!
acf_form_head();
$acf_form_args = array(
'id' => 'technic_edit_form',
'post_id' => 'technic_'.$term_id,
'form' => true,
'submit_value' => 'Update technic',
'field_groups' => array($group_ID),
'updated_message' => 'Technic is updated!';
);
acf_form( $acf_form_args );
Now your term's custom fields will be shown in this form. But to save term data after editing you'll need to add some more code. ACF form assumes that you're saving post data, we'll add some logic to detect saving data for term.
add_filter( 'acf/pre_save_post', 'acf_handle_form_save', 10, 1 );
function acf_handle_form_save( $post_id ) {
// Function accepts id of object we're saving.
// All WordPress IDs are unique so we can use this to check which object it is now.
// We'll try to get term by id.
// We'll get term id with added taxonomy slug, for example 'technic_405'.
// For checking term existence we must cut out this slug.
$cut_post_id = str_replace( 'technic_', '', $post_id );
$test_tax_term = get_term_by( 'id', $cut_post_id, 'technic' );
// If $test_tax_term is true - we are saving taxonomy term.
// So let's change form behaviour to saving term instead of post.
if ( $test_tax_term ) :
// Get array of fields, attached to our taxonomy
global $wpdb;
$group_ID = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_title = 'Technic CPT'" );
$acf_fields = acf_get_fields_by_id( $group_ID );
// Then sanitize fields from $_POST
// All acf fields will be in $_POST['acf']
foreach ( $acf_fields as $acf_field ) :
$$acf_field[ 'name' ] = trim( esc_attr( strip_tags( $_POST[ 'acf' ][ $acf_field[ 'key' ] ] ) ) );
endforeach;
// We need to have some fields in our group, which are just duplicates of standard term fields: name, slug, description.
// In this example it's only one field - term name, called 'technic_name'.
$name = 'technic_name';
// Update base term info, in this example - only name.
$term = wp_update_term( $cut_post_id, 'technic', array( 'name' => $$name ) );
// If all is correct, update custom fields:
if ( !is_wp_error( $term ) ) :
foreach ( $acf_fields as $acf_field ) :
update_field( $acf_field[ 'name' ], $$acf_field[ 'name' ], 'technic_' . $cut_post_id );
endforeach;
endif;
else :
// Here is saving usual post data. Do what you need for saving it or just skip this point
endif;
return $post_id;
}
Please note: validation of $_POST data may be more complex. For example, you may have to validate array of values if there are ACF galleries or relationships among your taxonomy fields. In my example i have only common text fields.
Hope that helps!
The answer from Alhana worked for me with one change. The term object works if sent as the the value for the post_id:
$term_obj = get_term($term_id);
$acf_form_args = array(
'post_id' => $term_obj,
'post_title' => false,
'submit_value' => 'Update Term',
'field_groups' => array($group_ID),
);

obtain related posts by tags

I'm integrating all posts from another site, and I want to show related posts of one of these posts , but only related posts of the same custom post type...
So, when I'm adding these custom posts, I'm adding custom post meta, and I'm saving these like a json. For example:
meta_key = "post_tags";
meta_value = "["tagA","tagB"]";
But I can't obtain related posts. I'm trying:
global $post;
$tags = get_post_meta($post->ID, "post_tags", true); //this is the json
if ($tags) {
$args=array(
'meta_key' => 'post_tags',
'meta_value' => $tags, // how compare with others posts???
'post__not_in' => array($post->ID), //not the same post
'posts_per_page'=>5, // Number of related posts to display.
'caller_get_posts'=>1
);
$my_query = new wp_query( $args );
if($my_query->post_count > 0){
?> <h3>Related posts</h3> <?php
}else if($my_query->post_count == 0){
?> <h3>There is not related posts</h3> <?php
}
But it always displays "There is not related posts"....when I have same posts with the same meta_key 'post_tags' (["tagA","tagB"]).
thanks in advance, Daniel
Firstly, if you add a query parameter of 'post_type' you can return only posts of your custom post type.
Secondly, you could make your custom post type support post tags and then when you integrate the other posts you can just import the tags using...
wp_set_post_terms( $post->ID, $tags );
IF you still want to do it your way, saving the terms in a meta field, you will want to save the terms as a PHP array not a JSON array. before you save it as meta use the following...
$php_terms = json_decode( $original_json, true );
... to convert the json array to a php array. then you can run the same query as you have above. The problem is that where you have queried your terms wordpress is expecting a PHP array so this should fix your issue.
Hope that helps
Dan

SQL query to select products by tag in WooCommerce?

I have a WooCommerce store with products which are either tagged as a smartphone or tablet. I'm looking for a way of selecting all smartphones from the database so does anyone know the raw SQL query to select all WooCommerce products by their tag?
This will get you the results you want using a native WordPress query. Then you can traverse the results using the loop:
// Define Query Arguments
$args = array(
'post_type' => 'product',
'product_tag' => $tags
);
// Create the new query
$loop = new WP_Query( $args );
// Get products number
$product_count = $loop->post_count;
// If results
if( $product_count > 0 ) :
// Start the loop
while ( $loop->have_posts() ) : $loop->the_post(); global $product;
/** Do stuff here **/
endwhile;
else :
_e('No product matching your criteria.');
endif; // endif $product_count > 0
Credit goes to Remi Corson for this solution.

Parsing External Table Arguments in Wordpress

I have a client project that has posts assigned to their country of origin.
The client wants to be able to search the posts by continent and or region.
I have a separate table that assigns each country to its respective region, and I have the WP query that uses the resulting array:
$country_names = array('England','France','Germany',...); // this would be the result from fetching countries associated with a region.
$args = array(
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'country',
'value' => $country_names,
'compare' => 'IN'
)
)
);
$query = new WP_Query( $args );
What I'm having trouble with is the part in the middle. Normally, I'd use some standard PHP/MySQL to craft the array:
<?php
//Process incoming variable
if(!empty($_REQUEST['region'])){
$region = $_REQUEST['region'];
} else {
$region = NULL;
}
// Make a MySQL Connection
$query = "SELECT * FROM regions WHERE region='$region'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
echo $row['country'];
?>
However, I'm having trouble making it work within a WP template, since WP manages the incoming variables using its own internal functions.
Can anyone help me to connect the two together? I'm sure I'm overlooking some simple step here, or else I'm not using some built-in WP function.
Any help is appreciated.
Thanks!
ty
Add this in your functions.php
add_filter( 'query_vars', 'addnew_query_vars', 10, 1 );
function addnew_query_vars($vars)
{
$vars[] = 'region'; // region is the variable you want to add
$vars[] = 'anotherVar';
return $vars;
}
Then get it
$region=get_query_var('region')
Update
$regions = $wpdb->get_results("SELECT ".$region." FROM `".$wpdb->regions."`");
if($regions)
{
foreach($regions as $region)
{
// your code
}
}

query post to different role users in wordpress

Is it possible to show different posts to a different role users?? I need something like this
User A => Post A ||
User B => Post B
I can't use UserID, because there will be a lot of users. I was thinking something like this
<?php
if ( is_userA_logged_in() ) {
echo 'Welcome A user!'; //here comes the query
} elseif ( is_userB_logged_in() ) {
echo 'Welcome B user!'; // here comes the query
}
?>
I was reading on the codex but I only found the is_admin(), how can I call a new role?
I'd create the new roles in the functions.php with
<?php
add_role('userA', 'User A', array(
'read' => true, // True allows that capability
'edit_posts' => false,
'delete_posts' => false, // Use false to explicitly deny
));?>
See the current_user_can function.
if ( current_user_can( $role ) )
$q = get_query();
Also, check functions like get_currentuserinfo and get_userdata.