How to get full info from given site url in Wordpress - php

I need to get full info by given site url.
For example:
$href = 'http://example.com/news/news-1';
I can get post id by
$post_id = url_to_postid( $href );
Then I can get post type by
$post_type = get_post_type( $post_id );
But it gives me nothing if it's a category url. So is there any other way to get info on given site url? Is this page even exist? Is it a page or a post? Is it a category?

You should use get_queried_object it will returns the queried objevt type, id etc.
Here is an example:
$current_item = get_queried_object();

A lot of is_ functions help you to determine type of node (is_single, is_category etc - check https://developer.wordpress.org/reference/functions/page/31/ and next pages). Then you get all info about this node.

Related

How do I generate numeric titled wordpress posts?

I would like to automatically generate some sequential Wordpress posts that have numeric titles, for example "1", "2", "3"... etc.
I'm looking for an automatic system since I would like my posts to go from "1" to "999999".
No post content or meta is required, only the titles.
I'm aware of wp post generate but I have no idea on how to use it so that the post titles have the requirements explained above.
I'm working on a demo project so search engine optimization is not a concern at the moment.
CLI usage is accepted.
You can achieve that quite simply by using a for loop with any arbitrary range (1-99 for example - or any other range you prefer)
for i in {1..99}; do wp post create --post_title="This is post number $i" --post_status=publish; done
This will call the wp post create command on each iteration of the loop and append the number to the post title.
<?php
// Create post object usinf for loop
for($i=1;$i<=9999;$i++){
$my_post = array();
$my_post['post_title'] = '<?php echo $i;?>';
$my_post['post_content'] = 'This is my post-<?php echo $i;?>';
$my_post['post_status'] = 'publish';
$my_post['post_author'] = 1;
$my_post['post_category'] = array(0);
// Insert the post into the database
wp_insert_post( $my_post );"
}
?>
wordpress appends a numeric value automatically if there's an existing post with the same post-title.
so if the permalinks are set to be postname the URL will look as follow:
my-post
my-post-1
my-post-2
etc

SearchWP Wordpress Plugin - Supplemental Search Engines - Custom Post Type

I have SearchWP included on my site. It works fine, with one exception: Supplemental Search Engine for Custom Post Type. I have implemented search engine for standard posts post type, which are my News with Setting up a Supplemental Search Engine: Step by Step tutorial.
Code in template that returns my results for News is:
global $post;
// retrieve our search query if applicable
$query = isset( $_REQUEST['swpquerypub'] ) ? sanitize_text_field( $_REQUEST['swpquerypub'] ) : '';
if( class_exists( 'SearchWP' ) ) {
$engine = SearchWP::instance();
$supplementalSearchEngineName = 'wyszukiwanie_publikacji';
$posts = $engine->search( $supplementalSearchEngineName, $query, $swppg );
}
and it works perfect.
But if I do same thing with my post type "publication" in archive-publication.php, I'm not getting results except one: my current page object. I think my problem is global $post call in custom post type loop. Anyway, as far as I know, I can call global $post inside loop of current post type, and it will return my post type objects, but it seems to work different. I'm trying to solve this for long time, and for now, I didnt find solution, so I'm asking you for help.

Wordpress: retrive an array with all post galleries images inside attachment.php

I'm building a custom gallery slider for a Wordpress theme and I have an architecture problem:
While I'm in the post template (single.php) I can easily retrieve an array with all post's galleries images via this code
$galleries = get_post_galleries( $post, false);
(BTW: False parameter is to have theirs url instead of the images themselves)
but when I click on a specific gallery's image, and I'm redirected to the attachment template (attachment.php), then it's impossible to have that same array.
I tried with:
$galleries = get_post_galleries( $post->post_parent, false);
but this doesn't work properly. Indeed if I build a gallery with some pictures which were originally attached to another post (a older one, for example), the post_parent parameter will refer to that old post, instead of the one which redirected me to the attachment template.
Well, this is a problem because my slider script is loaded in the attachement.php and it can't handle the right array of pictures.
I can't trigger it while in single.php because the slideshow start after clicking on a gallery image.
(For the moment I discard the idea of making a more complex script that avoid the loading of attachment.php tempalte).
I'm looking for a workaround to retrive in PHP the right array while in attachment template.
I managed to accomplish that in this way, inside the loop, in attachment.php:
// switch to the parent post, the one holding the [gallery] shortcode(s)
$post = get_post( wp_get_post_parent_id( get_the_ID( ) ), OBJECT );
setup_postdata( $post );
// get the galleries
$galleries = get_post_galleries( $post, false );
// VERY IMPORTANT: restore the original post (the attachment)
wp_reset_postdata( );
Personal note: I think the bug resides in the chain of calls:
get_post_galleries, do_shortcode_tag, gallery_shortcode
not transmitting the post ID parameter correctly so that at a certain point, the attachment ID is used instead of the one supplied by the user in the first get_post_galleries call.

Wordpress – Country Sensitive Post View

I'm looking for a solution to display posts based on the viewers
location.
For Example: I'm writing an article and set an option so it is only
visible for users from within USA.
If a user in Germany is visiting the Wordpress page, the post does
not appear.
I don't want to change content within a post, but more hide and show
them with an option for the author.
If there is some plugin, that can do this, I would highly appreciate.
Thanks.
I had similar problem. Didn't found a plugin and wrote this to query post for country of visitor: (remember to get your key at http://ipinfodb.com/)
$ip=$_SERVER['REMOTE_ADDR'];
$request = 'http://api.ipinfodb.com/v3/ip-country/?key= get your own key &ip='.$ip.'&format=json';
$response = file_get_contents($request);
$jsonobj = json_decode($response);
if ($jsonobj->countryCode == "SE"){
// args for query post for selected country $args = array ()
}
elseif ($jsonobj->countryCode == "PL"){
// args for query post for selected country $args = array ()
}
query_posts( $args );
Have a look at this discussion over here: https://wordpress.stackexchange.com/questions/26832/display-only-certain-posts-based-on-visitors-country. It might not give you a ready-to-use solution but maybe it helps as a pointer in the right direction.

How to get id of post inside of plugin

I want to retrive post id inside plugin.
I tried
global $post;
$a_Id=$post->ID;
and
global $wp_query;
$thePostID = $wp_query->post->ID;
and
var_dump(get_the_ID()); //shows just null
How i can retrieve it?
The idea is to get language of post from Custom Fields
and feed it into Global Translator plugin as a BASE LANG
EDIT:
I can retrive id from $_GET['p'] on development server but on production i
have pretty urls so i dont have it.
Assuming that you know what the ID is for an particular post you could use
var_dump(get_defined_vars());
to show a listing of anything that you can get your hands on.
Look through the output for the ID that you know and then use the path that it shows to get there.
What are you doing with the post ID? When exactly do you need it? I'm guessing you are using this very early in your plugin file, when the query is not yet parsed. The query is parsed after the hook parse_query is fired.

Categories