WordPress XMLRPC - Search Post by Slug and Update - wordpress

Is there anyway to search posts by slug through XMLRPC
https://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.getPosts
getPosts() doesn't seem to return using "name"..
$args = array(
'name' => 'my-slug',
'number' => 1
);
$post = $wpClient->getPosts( $args );
Please let me know if there is a workaround for this, I need to search by slug and then update those slugs remotely via XMLRPC. cheers

I ended up using Methods, this may help someone and save time.. paste the following code in functions.php of the domain you are fetching data from
add_filter('xmlrpc_methods', 'clx_xmlrpc_methods');
function clx_xmlrpc_methods($methods) {
$methods['getPostBySlug'] = 'clx_getpost';
return $methods;
}
function clx_getpost($args) {
global $wp_xmlrpc_server;
$slug = $args["slug"];
$pargs = array(
'name' => $slug,
'post_type' => 'post',
'numberposts' => 1
);
$my_posts = get_posts($pargs);
if( $my_posts ) :
return $my_posts; //echo $my_posts[0]->ID;
endif;
}
from your XMLRPC code use the following to get POST array from slug
$args = array(
'slug' => 'your-post-slug'
);
$postArray = $wpClient->callCustomMethod( 'getPostBySlug', $args );

Related

Wordpress JSON api fetch selected items

How can I fetch only post title, excerpt of all the posts of my WordPress blog using JSON api.
Currently, I am using https://www.example.com/wp-json/wp/v2/posts, which returns a lot of data that slows down the whole process. Is there any url where I can fetch only selected fields?
Have you considered writing your own API endpoints? https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/
Something like this would probably work for you where your endpoint becomes http://example.com/wp-json/myplugin/v1/post-titles:
function my_awesome_func( $data ) {
$args = array(
'post_type' => 'post',
);
$query = new WP_Query( $args );
$arr = array();
while ( $query->have_posts() ) {
$query->the_post();
$titles = get_the_title();
array_push($arr, $titles);
}
return $arr;
}
add_action( 'rest_api_init', function () {
register_rest_route( 'myplugin/v1', '/post-titles', array(
'methods' => 'GET',
'callback' => 'my_awesome_func',
) );
} );

How do I add a new page using code thru a Wordpress plugin?

I followed the chosen answer here -> How to create new page in wordpress plugin?
and I added the following code in a new Wordpress plugin folder and file and then activated in the Wordpress admin menu. Yet I don't have a new page created when I go to the slug demosite.com/custom/
add_action( 'admin_menu', 'register_newpage' );
function register_newpage(){
add_menu_page('custom_page', 'custom', 'administrator','custom', 'custompage');
remove_menu_page('custom');
}
Do I have to do something special to make my Wordpress plugin code work? I really need to be able to add a new page using my plugin functionality.
For create fronted page when plugin activation used register_activation_hook() like below.
register_activation_hook() function registers a plugin function to be run when the plugin is activated.
The first thing we do on activation is check that the current user is allowed to activate plugins. We do this using the current_user_can function
Finally, we create our new page, after we check that a page with the same name does not exist
register_activation_hook( __FILE__, 'register_newpage_plugin_activation' );
function register_newpage_plugin_activation() {
if ( ! current_user_can( 'activate_plugins' ) ) return;
global $wpdb;
if ( null === $wpdb->get_row( "SELECT post_name FROM {$wpdb->prefix}posts WHERE post_name = 'new-page-slug'", 'ARRAY_A' ) ) {
$current_user = wp_get_current_user();
// create post object
$page = array(
'post_title' => __( 'New Page' ),
'post_status' => 'publish',
'post_author' => $current_user->ID,
'post_type' => 'page',
);
// insert the post into the database
wp_insert_post( $page );
}
}
Here is a full list of parameters accepted by the wp_insert_post function
After plugin active successfully you can access your page using demosite.com/new-page-slug/
I'm not sure if you intended for the page to be created only once if so you should do it during plugin activation.
You might want to consider the following pseudo-ish code:
register_activation_hook( __FILE__, 'moveFile' );
function moveFile(){
if( check if post exists ){
wp_insert_post() # obviously title is "whatever", following convention
#move the file to themes folder
$source = plugin_dir_path(__FILE__) . "page-whatever.php";
$destination = get_template_directory() . "/page-whatever.php";
$cmd = 'cp ' . $source . ' ' . $destination;
exec($cmd);
}
}
It's similar to the code answered by Ankur, but this sample let you have a custom page. Caveat, my method uses exec() command.
I hope this helps.
/* If you want create a page with syn page template on plugin activation so see below example */
register_activation_hook( __FILE__, 'activate' );
function activate() {
$the_slug = 'our-services';
$args = array(
'name' => $the_slug,
'post_type' => 'post',
'post_status' => 'publish',
'numberposts' => 1
);
$my_posts = get_posts($args);
if(empty($my_posts)){
$my_post = array(
'post_title' => 'Our Services',
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'page',
'post_name' => 'our-services'
);
// Insert the post into the database
$post_id=wp_insert_post( $my_post );
update_post_meta( $post_id, '_wp_page_template', 'page-templates/our-services.php' );
}
}
/* page-template - our-services.php* /
<?php
/*
* Template Name: our-services
*/
get_header();
get_footer();
?>

Does "Advanced Custom Fields" taxonomy field support user pages?

I have a "taxonomy" custom field for the user pages. I want to build a query filtered by this field. It works with normal querys but not with user-querys, am i doing something wrong?
<?php
$args = array(
'key' => 'fruits',
'value' => 'apple'
);
// The Query
$user_query = new WP_User_Query( $args );
// User Loop
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) {
echo $user->display_name;
}
} else {
echo 'No users found.';
}
?>
Try get_users instead:
$users = get_users(array(
'meta_key' => 'fruits',
'meta_value' => 'apple'
));
var_export($users);
Wordpress codex: get_users()
Edit:
After a bit of research it turns out that get_users() is only a wrapper for WP_user_query, so switching to this function will make no difference.
However... did you notice that in my answer (and vrajesh') we have substituted your key with meta_key, and value with meta_value ... They are definitely defined in the WP_User_Query class, so I would be surprised if they didn't have any meaning.
If by chance you are using your original $args (which I guess does not actually refer to fruits and apples), then that may well be the explanation you are getting nothing.
try this:
$args = array( 'meta_key' => 'fruits', 'meta_value' => 'apple','compare' => '=');
Use WP_Query instead of WP_User_Query. WP_User_Query is used to retrieve data from user and usermeta table. And according to my understanding your are retrieving data from posts and postmeta table.
Class Reference/WP User Query and
Class Reference/WP Query
UPDATED:
Try this
<?php
$args = array(
'meta_query' => array(
'key' => 'fruits',
'value' => 'apple',
'compare' => '='
)
);
// The Query
$user_query = new WP_User_Query( $args );
// User Loop
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) {
echo $user->display_name;
}
} else {
echo 'No users found.';
}
?>

Wordpress wp_query and custom fields

I have a category, where posts are listed by this wp_query:
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'stage' => 'process',
'paged' => $paged,
);
$query = new WP_Query( $args );
Using plugin m_snap (alphabetical order of posts) there is a problem. It provides custom field for any post with a letter. So, the URL becomes something like this:
[...]/category/product/letter/A/ but has no difference with [...]/category/product/
How can I correct wp_query to get proper list of posts using m_snap?
UPD. Found that it's something wrong with rewriting urls. This is code from plugin:
function m_snap_generate_rewrite_rules($wp_rewrite) {
$IIIIIIIII1ll = array (
'letter/(.+)' => 'index.php?letter=' . $wp_rewrite->preg_index(1),
'tag/(.+?)/letter/(.+)' => 'index.php?tag='.$wp_rewrite->preg_index(1).'&letter='.$wp_rewrite->preg_index(2),
'category/(.+?)/letter/(.+)' => 'index.php?category_name='.$wp_rewrite->preg_index(1).'&letter='.$wp_rewrite->preg_index(2)
);
$wp_rewrite->rules = $IIIIIIIII1ll + $wp_rewrite->rules;
}
...
add_action ( 'generate_rewrite_rules', 'm_snap_generate_rewrite_rules' );
Any ideas to correct?

exclude a post from wp_query loop wordpress

I show my last post in a page with this code:
$query1 = new WP_Query();
$query1->the_post();
and it further with:
$id = $query->ID;
to retrive last post ID
so I wrote a new wp_query and I want to exclue that ID from the results:
I wrote this but it don't work:
$query2-> new WP_Query('p=-$id');
what's the problem?
You haven't excluded anything. Read the Codex. p= includes posts. It does not exclude them. What you need is post__not_in
$query2-> new WP_Query(array('post__not_in' = array($id)));
My code is works fine:
$ID =array('1,2,3,4,5');
$news = new WP_Query(array('
'post_type' => 'post',
'showposts' =>3,
'order' => 'DESC',
'post__not_in' => $ID
));
if ( $news->have_posts() ) :
echo '<div>';
while ( $news->have_posts() ) : $news->the_post(); ?>`
//Your code here

Resources