Wordpress - Retrieve blogroll links in PHP Array - wordpress

Wordpress Experts,
I am trying to retrieve all blogroll links in PHP array but not having any luck. I found function for retrieving in HTML code. Is there any way to retrieve list in array?
Thanks,
Mihir

But of course. The get_bookmarks function will retrieve links from the dashboard's Links section.
Default arguments and use are as follows (taken from the above link):
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'limit' => -1,
'hide_invisible' => 1,
'show_updated' => 0);
$bookmarks = get_bookmarks( $args );
foreach ( $bookmarks as $bm ) {
// Do something exceedingly clever
}

Related

How to filter custom fields for custom post type in wordpress rest api?

I use wordpress standard with the plugins "Advanced custom fields" and "custom_post_type ui". I created a post_type called deals and added some custom fields with it.
What I need to do now, is filter the results when accessing the rest api like this:
http://localhost:8000/wp-json/wp/v2/deals
Actually I only need the acf part of it. I dont care about the rest.
[{"id":29,"date":"2019-04-12T12:34:14","date_gmt":"2019-04-
12T12:34:14","guid":{"rendered":"http:\/\/localhost:8000\/?
post_type=deals&p=29"},"modified":"2019-04-
12T12:34:14","modified_gmt":"2019-04-12T12:34:14",
"slug":"test-title","status":"publish","type":"deals",
"link":"http:\/\/localhost:8000\/deal s\/test- title\/","template":"",
"meta":[],"tax-deals":[],"acf":{"title":"Title for Deals
Post","description":"","image":false,"date_start":"01.01.1970",
"date_end":"01.01.1970","category":"Kleidung"},"_links":{"self":
[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/deals\/29"}],
"collection":[{"href":"http:\/\/localhost:8000\/wp-
json\/wp\/v2\/deals"}],"about":[{"href":"http:\/\/localhost:8000\/wp-
json\/wp\/v2\/types\/deals"}],"wp:attachment":
[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/media?
parent=29"}],"wp:term":[{"taxonomy":"tax_deals","embeddable":true,
"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/tax-deals?
post=29"}],"curies":
[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}},
I have already tried using
http://localhost:8000/wp-json/wp/v2/deals?search=id
to get the id or something, but response is empty.
Also this didnt work:
http://localhost:8000/wp-json/wp/v2/deals?id=28
Again empty response.
To summarize: I need to filter my custom post type on my custom fields by the "acf" attribute shown in my response json. How does it work?
EDIT: I already installed "WP REST Filter" but still dont know how to do it.
I suggest you to create a new API where you can customize the output. Take advantage of wordpress function register_rest_route() using this you can create an API from CPT and ACF in one ajax url. And you do not need to install anything.
Check how I get my instructor CPT and mycheckbox ACF.
// your ajaxurl will be: http://localhost/yoursite/wp-json/custom/v2/instructor/
add_action( 'rest_api_init', function () {
register_rest_route( 'custom/v2', '/instructor', array(
'methods' => 'GET',
'callback' => 'instructor_json_query',
));
});
// the callback function
function instructor_json_query(){
// args to get the instructor
$args = array(
'post_type' => 'instructor',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'mycheckbox', // your acf key
'compare' => '=',
'value' => '1' // your acf value
)
)
);
$posts = get_posts($args);
// check if $post is empty
if ( empty( $posts ) ) {
return null;
}
// Store data inside $ins_data
$ins_data = array();
$i = 0;
foreach ( $posts as $post ) {
$ins_data[] = array( // you can ad anything here and as many as you want
'id' => $posts[$i]->ID,
'slug' => $posts[$i]->post_name,
'name' => $posts[$i]->post_title,
'imgurl' => get_the_post_thumbnail_url( $posts[$i]->ID, 'medium' ),
);
$i++;
}
// Returned Data
return $ins_data;
}
Then, you can use the link: http://localhost/yoursite/wp-json/custom/v2/instructor/ in your ajax url.

Display list of woo commerce categories by user defined order

I want to display all woo commerce categories by user defined order. As an example . I tried the following code:
$args = array(
'number' => $product_number,
'order' => 'asc',
);
$product_categories = get_terms('product_cat', $args);
This code works fine and returns an array or all category name in ascending order. What I want now is allow users to pass an array of category ids and display category list by the supplied id order. Is that possible ? Did some research but can not find any close solution.
$product_number = 10; // Any number you have defined
$catsArray = array(1,2,3,4,5,8,10,20); // User provided array of terms ids
$product_categories= get_terms( array(
'number' => $product_number,
'taxonomy' => 'product_cat',
'include' => $catsArray,
'hide_empty' => false,
'orderby' => 'include',
'order' =>'ASC'
) );
Now you can get the categories in
You can do it with php loop. Which takes default ordered term array and build new, custom ordered array on it.
$user_arg = array(1,2,4,5);
$product_categories = get_terms('product_cat', $args);
$temporary_array=array();
foreach ($product_categorie as $pcat) {
$temporary_array[$pcat->term_id]=$pcat;
}
$final_array=array();
foreach($user_arg as $ua){
$final_array[$ua]=$temporary_array[$ua];
}
Now $final_array contains same data with $product_categories, but in custom order based on $user_arg.

WordPress: Taxonomy archives based on Custom Post Type

I've generated three different custom post types (e.g. books, movies, games).
And I've a custom taxonomy for all of them (e.g. genre).
What I need are archives for the taxanomy based on the post types.
For example: "books-genre", "movies-genre"...
Is there any solution to do that? Now I've only the taxonomy archive for "genre".
The way I like to approach custom post archives is to create a custom archive template with WP_Query sections where I need them. You'd create the blank file in the root of your theme at archive-cptnamehere.php.
You might have some template partials to add in but the core of the page looks like this:
<?php
// 1- Get ID of the page's path by URL (reliable)
$pagePath = $_SERVER['REQUEST_URI'];
$pageObject = get_page_by_path($pagePath);
$pagePathID = $pageObject->ID;
// 2- Print page title
$teamPageTitle = get_the_title($pagePathID);
echo $teamPageTitle;
// 3 - Do a query that gets the data you need
// Args: -1 shows all locations, orders locations alphabetically by title, orders locations a-z, only query against team items
$args = array(
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'post_type' => 'team',
'meta_query' => array(
array(
'key' => 'team_page_categorization',
'value' => $team_page_categorization_options_array_item,
'compare' => 'LIKE'
)
)
);
$the_query = new WP_Query( $args );
// 4- Setup query while loop
if($the_query->have_posts()) {
while($the_query->have_posts()) {
$the_query->the_post();
// 5- Do what you need to inside the loop
// 6- Close it all up, don't forget to reset_postdata so you can do additional queries if necessary!
}
wp_reset_postdata();
}
?>

WP_Query ordering by "rand" and "name"?

I am using Wordpress and for the posts on my homepage, I want them to get ordered both randomly and by name. What I mean is, I want to show different posts on my home page each time, yet ordered by their names. I changed the WP_Query args from my theme files as below, but it didn't work. I'm getting unrelated results for a reason I can't understand.
#setup wp_query
$args = array(
'posts_per_page' => $postsperpage,
'orderby' => array( 'rand', 'title' ),
'order' => 'DESC',
);
Is there any way to make that possible?
p.s. I am honestly sick of those who downvote the questions irrelevantly. I wouldn't have asked the question should i had found a solution on the internet. If you have a logical reason please either warn me or edit my post instead of blindly downvoting it.
As documented:
orderby (string | array) - Sort retrieved posts by parameter. Defaults to 'date (post_date)'. One or more options can be passed.
So:
$args = array(
'orderby' => array( 'rand', 'name' ),
'order' => 'DESC',
);
But I don't think this will get you the result you desire. You will most probably have to only use rand with posts_per_page setting to get your desired number of posts. Fetch all posts and sort these by name afterwards.
Example:
$args = array(
'orderby' => 'rand',
);
// get X random posts (10 by default)
$result = new WP_query( $args );
// sort these posts by post_title
usort( $result->posts, function($a, $b) {
if ( $a->post_title == $b->post_title )
return 0;
return ($a->post_title < $b->post_title) ? -1 : 1;
} );
// Start the loop with posts from the result.
while ( $result->have_posts() ) : $result->the_post();
// do your stuff in the loop
}

how to add new pages using WP core function

how can I add a page using core functions of wordpress.I can find function for Post etc as well but couldn't find any for Pages.Any one who can help me
Thanks
You use the same function to add posts or pages, just add the post_type parameter like this :
$args = array( 'post_type' => 'page',
'post_content' => 'You content...',
'post_parent' => $parent_id; // ID of the page this one should be a child of
... // etc.
...
'post_title' => 'Title for your page');
wp_insert_post ($args);

Resources