Android application download wordpress content - php

I have an android application that need to download (via webservice - php ) some books from a wordpress website.
Those books are structured like this : Book Name -> Category 1 -> Subcategory 1 -> text
All information is stored in wp_posts and all realtions are made with wp_postmeta.
I try to begin from "top-to-bottom" starting with "Book Name" like this :
$args = array(
'posts_per_page' => 8,
'orderby' => 'rand',
'post_type' => 'biblie',
'post_status' => 'publish'
);
$show_albums = get_posts( $args ));
and then with wp_query() using post_type, meta_key,meta_value
But any step i take with this method it gets more and more puzzling
So .... is there a way to have all the information using one interogation? I want to interogate wp by book name and somehow have all the information resulting in one arrayor it is to much from wordpress ?
Hope you understand my english !
Thanks

There are plugins that give you a restful api out of the box which could do what you want. A quick search gave me these:
https://wordpress.org/plugins/json-api/
https://github.com/WP-API/WP-API

Related

Category name in WP query should dynamically come

<?php wp_dropdown_categories(); ?>
→ This is how we can populate the categories in a drop-down.
N.B. We are dealing with WordPress.
This is how we generate a query in Wordpress:
<?php
// the query
$the_query = new WP_Query( array(
'post_type' => 'post',
'posts_per_page' => 10,
'post_status' => 'publish',
'category_name' => 'staff'
) );
?>
I need some help in php here →
'category_name' => 'staff'
staff is a category, for example, here. How can we write in terms of PHP or Ajax so that whatever category is chosen from the populated category from the drop-down should come above dynamically based on the selection from the drop-down?
Means → 'staff' This should come up dynamically from the drop down. I am normal in PHP and ajax If someone can guide me than help will be highly appreciated.
As we can see in the documentation of wp_dropdown_categories the input name is cat (or you can change it to anything you like and does not affect other parts of your code).
So in your PHP file do sth liek the following:
<?php
// the query
$the_query = new WP_Query( array(
'post_type' => 'post',
'posts_per_page' => 10,
'post_status' => 'publish',
'category_name' => $_POST['cat'] // or $_GET['cat'] or $_REQUEST['cat']
) );
?>
Make sure to validate and sanitize your POST/GET input to avoid problems
[UPDATE] If you use ajax then you have one php file that handles the ajax request using an action (for example take a look here). Then in your web facing php page use jquery or similar to post (or get) the ajax request and return the categories or posts using WP_Query like shown above. Read the article mentioned to have an idea how to implement ajax in Wordpress.
If you have further questions with the implementation please provide specific information so it can be helpful.

How to post to different categories in wordpress using a custom php script

i am developing a custom php script outside wordpress. The purpose of this script is to post to post articles on my wordpress blog.
I have almost achieve that put the only fuction remaining is that the post doesn't appear in any category or tag.
I have only been able to connect to the database and save data into the wp_posts table.
But i don't now how to go about accessing the database and make the post have a category and tag.
If you are wondering why am creating a custom php script for posting is that i added some functionality which i want during posting and made it lighter than the normal wordpress post-new.php.
First of all , You are not obliged to reinvent the wheel and do your own sql queries using MYSQLI or PDO , Or using anything else ,
All you need to do is just
Require wp-load.php file into your custom php script , Then you can use many wordpress builtin functions , After that just use the function
wp_insert_post($args)
, in the args "array" include all the data you need regarding the post , Starting from the post author , post content , category , post excerpt and so on ..
Here is an example :
// Create post object
$my_post = array(
'post_title' => wp_strip_all_tags( $_POST['post_title'] ),
'post_content' => $_POST['post_content'],
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 8,39 )
);
// Insert the post into the database
wp_insert_post( $my_post );
Check wordpress codex for more info :
https://developer.wordpress.org/reference/functions/wp_insert_post/

Wordpress: Custom WP_Query returning first page posts for all 'paged' parameter values

I'm trying to create a "load more posts" button for my Wordpress blog page using AJAX. The Javascript function that takes care of passing the 'paged', 'offset' and 'posts_per_page' parameters is doing so successfully (I've checked a hundred times!).
The offset and posts_per_page parameters work fine (I repeatedly changed their values to test this) but the WP_Query always returns the posts on the front page, even though the value of $page changes each time more posts are loaded.
Just incase the $paged value was getting messed up somewhere, I used static numbers for the 'paged' parameter but it still only shows first page posts no matter what the value.
Everything executes fine and I receive no error message, with debugging enabled. The posts do load - just not the right ones.
Other info: my own theme, no plugins running right now.
The code below is from a function in my functions.php file.
$args = array(
'suppress_filters' => true,
'post_type' => 'post',
'orderby' => 'date',
'order' => 'desc',
'posts_per_page' => $ppp,
'paged' => $page,
'offset' => $offset, //I've tried simply putting 3, 5 etc
);
$loop = new WP_Query($args);
if ($loop -> have_posts()) :
while ($loop -> have_posts()) : $loop -> the_post();
the_title();
endwhile;
endif;
wp_reset_postdata();
I've only entered what I believe to be relevant code, in order to make it easier to read. Let me know if you'd like me to add something. And thank you!
EDIT: Using the query() function directly makes it work but I really don't want to mess with the main query object...and this just means that everything else in my code is fine. WP_Query is just being weird.
You can't used offset and paged together - you would only use one or the other. Using them together breaks pagination. If you want the posts that would appear on page 2 (assuming you have 10 to a page), use either:
'paged' => 2
or
'offset' => 10

Automated featured product woocommerce

This is my first post in this great community. Normally I find all the answers to my small and large problems, but this I'am sad to say is a mystery to me :(
I need to create some kind of automated function that every day, once a day chooses 3 random products and marks them as featured in my Woocommerce store. And the next day removes these 3 and chooses 3 new once.
Sadly due to complicity of the task(for my), I have no code to show/start from.
Only thing I can find is that it has to be defined in some meta information:
$args = array(
'post_type' => 'product',
'meta_key' => '_featured',
'meta_value' => 'yes',
)
Truely hope someone can help me :)
I think the easiest solution would be to use transients.
Here's a suggestion, but first a couple of caveats.
if no one visits your site the transients don't always clear at the right times, but if that's an issue you can set up a ping service to automatically hit your site every day.
you don't have precise control over when the transient clears... ex: every day at 3pm.
This suggestion requires you to rework how your theme is displaying these 3 featured products. since we aren't actually looking for the _featured meta.
code for functions.php:
function so_35312355_get_daily_featured_products(){
// Get any existing copy of our transient data
if ( false === ( $daily_featured_products = get_transient( 'daily_featured_products' ) ) ) {
// It wasn't there, so regenerate the data and save the transient
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'orderby' => 'rand',
'posts_per_page' => '3',
);
$daily_featured_products = new WP_Query( $args );
set_transient( 'daily_featured_products', $daily_featured_products, 24 * HOUR_IN_SECONDS );
}
return $daily_featured_products;
}
Alternatively, you could run 2 queries.... one to reset all the featured meta keys and a second to update the meta keys for each of your random 3 products.
A more advanced and complete method would be to use cron jobs. Take a look in the Codex for how to schedule an event, but depending on your needs the transient may be enough.

wp_query maximum limit without pagination?

My client has a custom post type called 'Clients'. She uses this to record all information about each client she has. She then sometimes leaves these in either draft mode or published. She would like to be able to see all of her clients at a glance and copy and paste all of their details straight from the web page on a regular basis. I have created the following query (note the query is only showing fist and last name at the moment for testing):
<?php
$args = array(
'post_type' => 'clients',
'post_status' => array('draft', 'publish'),
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page' => -1
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$fname = get_field('fname');
$sname = get_field('sname');
echo '<p>'.$fname.', '.$sname.'</p>';
endwhile;
?>
However, this returns no results. When I change the 'posts_per_page' to a number, the query works. However, if this number is above 58 no results are returned again. I think somewhere there is a limit to how much can be output in 1 query. I cannot use pagination at the minute as the client is admit she'd want to see all information on one page. Is there another way of querying this without limits? Or a way to adjust the limit?
Thank you in advance
Without being able to debug it directly I'm not sure why you're seeing this issue. Alternatively you can try using the 'nopaging' => true option instead of the 'posts_per_page' option. Does that method behave?
Are you sure that the custom post type name is clients and not client? That is the only thing that I can point, as I the only parameter that is custom by the developer ^^.
The problem was down to my local setup, as soon as it was uploaded to the clients server the error disappeared. Sorry I forgot to close the question

Categories